> 文章列表 > unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

如代码所示,简单了解一下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class test : MonoBehaviour
{   void Awake(){Debug.Log("awake hello world!");}// 当脚本可用时,也就是打勾的时候可以使用void OnEnable(){Debug.Log(" OnEnable"); }// Start is called before the first frame update// 这个也是只会初始化一次void Start(){Debug.Log("start");}// Update is called once per frame,下面两个函数都和帧相关,一先一后的调用关系void Update(){Debug.Log("update");}private void LateUpdate(){Debug.Log("LateUpdate");}// 这个是定时进行刷新private void FixedUpdate(){Debug.Log("FixedUpdate");}private void OnDisable(){}// 销毁,移除脚本就认为是销毁了组件,或者启动关闭了private void OnDestroy(){Debug.Log("OnDestroy");}
}

编辑脚本的执行顺序

我个人感觉不要一个放在Awake函数中,一个放在Start中。因为这只适合两个脚本使用,如果多个脚本还是没有办法解决脚本执行的顺序。在这里设置脚本的执行顺序,添加进去的值越小在队列中越靠前越先执行

unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

物体标记

给标签换个颜色就能看见文本了。

unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

图层

可以根据不同图层来做碰撞检测,目前还没有做,简单的了解一下

预设体

修改预设体的值,大家都变了,这样省的一个变了,其他的还要手调,但如果只需要这某一个物体变了,就不要改变预设体的值,而是单独操作这个对象。要改变预设体就直接改右边,如果改单独的一个对象,就改左边的值。试了下材质是一改都要改的,只动一个是不可以的。
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

找不到预设体怎么办?

找不到对应的预设体就点一下这个选择
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

修改一个预设体物体不操作预设体本体怎么做

点一下打开,这样操作就会直接影响预设体的本体从而影响所有采用预设体的物体了。
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

预设体变体

相当于多个基类的感觉,图标变的有点灰色
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

欧拉与四元数转换

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RotateTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){// 欧拉角,四元数  // unity 用的是四元数Vector3 v = new Vector3(0, 30, 0);Quaternion q = Quaternion.identity; // 四元数// 通过欧拉角创建四元数q = Quaternion.Euler(v);// 看向一个物体,我猜打怪的时候,玩家视野自动移入q = Quaternion.LookRotation(v);// 四元数转为欧拉var v2 = q.eulerAngles;}// Update is called once per framevoid Update(){}
}

调试

我以为是代码进行debug,不是的,是画线来进行调试,我猜这样方便后面好学习视野的范围

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class DebugTest_22 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){// 绘制一条线Debug.DrawLine(Vector3.zero, Vector3.one,Color.blue);// 绘制一条射线Debug.DrawRay(Vector3.zero, Vector3.up, Color.red);}
}

获取对象实例

可以简写gameObject获取属性,C#有点厉害,类里可以不用写this指针直接调用类对象。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Empty_23 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){// 获取对象的这个实例GameObject go = this.gameObject;Debug.Log(go.name);// 简便写法Debug.Log(gameObject.name);// tag标签Debug.Log(gameObject.tag);// 图层Debug.Log(gameObject.layer);}// Update is called once per framevoid Update(){}
}

生成gameObject对象,可以在A物体操作B物体

声音一个对象即可,然后在unity编辑器上给他附一个新值

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Empty_23 : MonoBehaviour
{public GameObject myFirstNewGameObj;// Start is called before the first frame updatevoid Start(){Debug.Log(String.Format("myFirstNewGameObj name:{0}",myFirstNewGameObj.name));}// Update is called once per framevoid Update(){}
}

在unity编辑器这里设置上一个对象就可以正常使用了
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

对象是对象,组件是组件

我以为可以gameObject走天下,没想到大意了,并不能一个gameObject走天下,只有transform这个组件封了个对象,因为这是必需的,其他组件如果利用模板函数来生成一个对应的组件对象。

		// 获取transform组件Debug.Log(transform.position);// 获取其他的组件,用模板的方式来获得对象BoxCollider bc = GetComponent<BoxCollider>();

添加组件

注意这个只有编辑器运行时候才可以创建出来这个组件,如果是这样创建的话,我认为必需要在代码里做好相应的初始化,毕竟脚本也是运行时产生的结果

	// 添加一个组件gameObject.AddComponent<AudioSource>();

获取组件

通过名字,名字是全匹配,或者按照标签来找到这个对象。

	GameObject test = GameObject.Find("Test");GameObject test2 = GameObject.FindWithTag("Test");

生成预设体

我当时就在想,如果需要多个预设体总不能一直让我拖来拖去吧,不累死我,按照vue框架也是一个v-for循环进行渲染。跟生成gameObject一个套路,先定义一个对象,然后把预设体的组件拖过来,然后进行绑定,最后调用Instantiate这个函数生成一个预设体实体

 	// 生成一个预设体实体Instantiate(myPrefab);

空空如也
unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学

运行时就可以出现啦,我们使用代码创造的预设体实体

unity的学习,准备搞一款mmo小游戏,服务器和客户端从零学