元宇宙游戏链游系统开发丨元宇宙游戏链游系统源码案例开发

2023-02-21 14:51:19 浏览数 (2)

  什么是元宇宙?

  元宇宙指的是通过虚拟增强的物理现实,呈现收敛性和物理持久性特征的,基于未来互联网,具有链接感知和共享特征的3D虚拟空间。

  大概可以从时空性、真实性、独立性、连接性四个方面交叉描述元宇宙:

  (1)从时空性来看,元宇宙是一个空间维度上虚拟而时间维度上真实的数字世界;

  (2)从真实性来看,元宇宙中既有现实世界的数字化复制物,也有虚拟世界的创造物;

  (3)从独立性来看,元宇宙是一个与外部真实世界既紧密相连,又高度独立的平行空间;

  (4)从连接性来看,元宇宙是一个把网络、硬件终端和用户囊括进来的一个永续的、广覆盖的虚拟现实系统;

  UDP信号接收

代码语言:javascript复制
  public class UdpController:MonoBehaviour
  {
  //UDP Receiver
  private Socket socket;
  private EndPoint clientEnd;
  private Thread connectThread;
  //Controller
  private Animator _animator;
  private Rigidbody _rigidbody;
  private CharacterController _characterController;
  private Transform _transform;
  public float Yaw{get;private set;}
  public float jumpHeight=2.0f;
  public float walkSpeed=3.0f;
  public float rotateSpeed=10;
  public Photographer photographer;
  public Scrollbar speedScrollbar;
  public Scrollbar jumpHeightScrollbar;
  public Scrollbar rotateSpeedScrollbar;
  public void rotateSpeedScrollbarChanged()
  {
  rotateSpeed=rotateSpeedScrollbar.value*20;
  }
  public void speedScrollbarChanged()
  {
  walkSpeed=speedScrollbar.value*6;
  }
  public void jumpHeightScrollbarChanged()
  {
  jumpHeight=jumpHeightScrollbar.value*4;
  }
  void InitSocket()
  {
  IPEndPoint ipEnd=new IPEndPoint(IPAddress.Any,8051);
  socket=new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
  socket.Bind(ipEnd);
  IPEndPoint sender=new IPEndPoint(IPAddress.Any,0);
  clientEnd=(EndPoint)sender;
  Debug.Log("Waiting for UDP dgram");
  connectThread=new Thread(new ThreadStart(SocketReceive));
  connectThread.Start();
  }
  void InitRobot()
  {
  _animator=GetComponent<Animator>();
  _characterController=GetComponent<CharacterController>();
  _rigidbody=GetComponent<Rigidbody>();
  _transform=GetComponent<Transform>();
  }
  private delegate void VoidFunction();
  private Dictionary<string,VoidFunction>strToFunc;
  void InitDelegate()
  {
  strToFunc=new Dictionary<string,VoidFunction>();
  strToFunc.Add("Left",this.TurnLeft);
  strToFunc.Add("Right",this.TurnRight);
  strToFunc.Add("Idle",this.StandStill);
  strToFunc.Add("Running",this.MakeRunning);
  }
  private float mouseX=0;
  bool running=false;
  void MakeRunning()
  {
  Debug.Log("MakeRunning");
  mouseX=0;
  running=true;
  }
  void StandStill()
  {
  Debug.Log("StandStill");
  mouseX=0;
  running=false;
  }
  void TurnRight()
  {
  Debug.Log("TurnRight");
  mouseX=1;
  running=false;
  }
  void TurnLeft()
  {
  Debug.Log("TurnLeft");
  mouseX=-1;
  running=false;
  }
  void SocketReceive()
  {
  while(true)
  {
  byte[]recvData=new byte[1024];
  int recvLen=socket.ReceiveFrom(recvData,ref clientEnd);
  Debug.Log("message from:" clientEnd.ToString());
  String recvStr=Encoding.ASCII.GetString(recvData,0,recvLen);
  VoidFunction func;
  if(strToFunc.TryGetValue(recvStr,out func))func();
  Debug.Log(recvStr);
  }
  }
  void SocketQuit()
  {
  if(connectThread!=null)
  {
  connectThread.Interrupt();
  connectThread.Abort();
  }
  if(socket!=null)
  {
  socket.Close();
  Debug.Log("Socket Close!");
  }
  }
  private void Awake()
  {
  rotateSpeed=20;
  InitRobot();
  InitDelegate();
  InitSocket();
  }
  //Update is called once per frame
  void Update()
  {
  Yaw =mouseXrotateSpeedTime.deltaTime;
  photographer.SetYaw(Yaw);
  Quaternion rot=Quaternion.Euler(0,Yaw,0);
  _transform.rotation=rot;
  _animator.SetFloat("Direction",mouseX);
  if(running)
  {
  _rigidbody.MovePosition(_rigidbody.position Time.deltaTimewalkSpeed(rot*Vector3.forward));
  }
  }
  private void OnApplicationQuit()
  {
  SocketQuit();
  }
  }

0 人点赞