1. 学习资源:

    # c# 教程
    https://www.runoob.com/csharp/csharp-tutorial.html
    # lua 教程
    https://www.runoob.com/lua/lua-tutorial.html
    # xlua 教程
    https://github.com/Tencent/xLua
    # protobuf 教程
    https://www.tizi365.com/archives/367.html
    # unity 教程
    https://docs.unity3d.com/cn/2019.4/Manual/UnityManual.html
  2. 生命周期:

    Awake()  // 初始化脚本数据
    Start()  // 游戏初始化执行一次
    Update()  // 每帧运行
    FixedUpdate()  // 每帧运行,用于碰撞组件
    OnEnable()  // 游戏对象激活时调用
    OnDisable()  // 游戏对象禁用时调用
    OnTriggerEnter2D()  // 触发检测
    OnCollisionEnter2D()  // 碰撞检测
  3. Time 类:

    Time.deltaTime  // 固定时间
    Time.fixedDeltaTime  // 固定时间,用于碰撞组件
    Time.timeScale = velocity  // 控制游戏的运行速度:0: 暂停; >1: 快进; <1: 快退;
  4. GameObject 类:

    # 创建游戏对象
    new GameObject("对象名")  // 创建空对象
    GameObject.Instantiate(gameobj, position, rotation)  // 实例化预制体
    GameObject.CreatePrimitive(PrimitiveType.Cube)  // 创建系统自带几合体
    # 添加组件
    gameobj.AddComponent<组件名>()
    # 获取组件
    gameobj.GetComponent<组件名>()
    # 对象属性
    组件.gameObject  // 获取组件上的对象
    gameobj.transform.position  // 获取位置
    gameobj.transform.rotation  // 获取旋转角度
    gameobj.activeSelf  // 获取对象的激活状态
    gameobj.tag  // 获取标签
    gameobj.name  // 获取名称
    # 对象方法
    GameObject.SetActive(bool)  // 设置对象的激活状态
    Destroy(gameobj,time)  // 延迟 time秒后,删除对象
    DontDestroyOnLoad(gameobj)  // 加载新的场景后,不删除该对象
    GameObject.FindGameObjectWithTag("tagname")  // 通过标签查找游戏对象(仅匹配一个)
    GameObject.FindGameObjectsWithTag("tagname");  // 通过标签查找游戏对象(匹配所有)
    # 调用跟游戏对象绑定的脚本里面的方法
    gameobj.SendMessage(方法名,[参数])  // 调用对象自身绑定的脚本中的方法
    gameobj.BroadcastMessage(方法名,[参数])  // 调用对象及其子对象绑定的脚本中的方法(父调子)
    gameobj.SendMessageUpwards(方法名,[参数])  // 调用对象及其父对象绑定的脚本中的方法(子调父)
  5. MonoBehaviour 类:

    # 成员属性
    this.enabled = bool  // 设置当前脚本的激活状态
    this.tag  // 获取当前脚本所绑定对象的 标签
    this.name  // 获取当前脚本所绑定对象的 名字
    # 内置属性
    [HideInInspector]  // 在脚本面板中隐藏公有属性
    # 方法调用
    Invoke("方法名",time)  // time秒 后,调用脚本中定义的方法
    IsInvoking("方法名")  // 判断方法是否已调用
    InvokeRepeating("方法名", interval, time)  // time秒 后,调用脚本中的方法,每 interval秒,调用一次
    CancelInvoke("方法名")  // 取消调用
    # 鼠标事件(挂载碰撞体组件后生效)
    OnMouseDown  // 鼠标按下
    OnMouseUp  // 鼠标抬起
    OnMouseEnter  // 鼠标移入
    OnMouseExit  // 鼠标移出
    OnMousetOver  // 鼠标悬停
    OnMouseDrag  // 鼠标拖动
    OnMouseUpAsButton  // 鼠标按下并抬起
  6. Mathf 类:

    Mathf.Abs(num)  // 绝对值
    Mathf.Ceil(num)  // 向上取整
    Mathf.Floor(num)  // 向下取整
    Mathf.Clamp(value, min, max)  // 限制 value 的大小
    Mathf.Pow(m, n)  // 获取 m 的 n次方
  7. Input 类:

    Input.GetKeyDown(KeyCode.A)  // 按下
    Input.GetKey(KeyCode.A)  // 按住
    Input.anyKeyDown  // 任意键按下
    Input.anyKey  // 任意键按住
    Input.GetMouseButtonDown(num)  // 鼠标按下:0.左键 1.右键
    Input.GetAxis("Horizontal/Vertical")  // 获取虚拟轴的值
    Input.mousePosition  // 获取鼠标所在屏幕位置的像素坐标
  8. Vector3 类:

    Vector3.MoveTowards(from, to, Time.deltaTime)  // 匀速移动到目标对象的位置
    Vector3.Slerp(from, to, Time.deltaTime)  // 减速移动到目标对象的位置
    transform.position = new Vector3(x, y, z)  // 位移
    transform.rotation = Quaternion.Euler(new Vector3(x, y, z));  // 旋转
    transform.localScale = new Vector3(x, y, z)  // 缩放:x、y、z 其中一个值为 -1 时,翻转
  9. Rigidbody2D 类:

    Rigidbody2D.position  // 获取/设置刚体的位置
    Rigidbody2D.rotation  // 获取/设置刚体的旋转角度
    Rigidbody2D.velocity  // 获取/设置刚体的移动速度
    Rigidbody2D.angularVelocity  // 获取/设置刚体的旋转角度
    Rigidbody2D.useGravity = bool  // 设置刚体是否使用重力
    Rigidbody2D.MovePosition(target position)  // 移动刚体所绑定的对象到指定位置
    Rigidbody2D.MoveRotation(Quaternion.Slerp(self rotation, target rotation, Time.deltaTime))  // 旋转刚体所绑定的对象到指定角度
    Rigidbody2D.AddForce(force)  // 向指定方向施加力
  10. Random 类:

    Random.InitState((int)System.DateTime.Now.Ticks)  // 播种
    Random.Range(min, max)  // 生成
  11. Quaternion 类:

    四元数.eulerAngles  // 四元数转欧拉角
    Quaternion.Euler(欧拉角)  // 欧拉角转四元数
    Quaternion.LookRotation(target position - self position)  // 转向目标游戏对象
    Quaternion.Slerp(self rotation, target rotation, Time.deltaTime)  // 缓慢地转向目标游戏对象
  12. Application 类:

    Application.dataPath  // Assets 目录
    Application.persistentDataPath  // 写入数据的目录
    Application.OpenURL("http://www.baidu.com")  // 打开网址
    Application.isMobilePlatform  // 是否是移动平台
    Application.platform  // 获取当前平台
    Application.runInBackground  // 判断能否在后台运行
    Application.Quit()  // 退出游戏
  13. SceneManager 类:

    SceneManager.LoadScene(场景名)  // 加载场景
    SceneManager.sceneCount  // 获取已加载场景的个数
  14. AudioSource 类:

    AudioSource.isPlaying  // 判断是否正在播放
    AudioSource.Play()  // 播放
    AudioSource.Stop()  // 暂停
  15. Screen 类:

    Screen.width  // 获取屏幕的宽度
    Screen.height  // 获取屏幕的高度
  16. 杂项:

    • 加载 Resources 文件夹下的精灵
      SpriteRenderer sr = GetComponent<SpriteRenderer>();
      Sprite sprite = Resources.Load("picture name", typeof(Sprite)) as Sprite;
      sr.sprite = sprite;
    • UGUI -> Button 点击事件
        Button.onClick.AddListener(delegate () {
                          this.SomeMethod();  // 调用某个方法
                      });
    • 切换动画变量
      Animator.SetTrigger("some value")  // 设置 trigger 变量
      Animator.SetBool("some value",true)  // 设置 bool 类型变量
    • 射线检测
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit.collider != null)
            {
                // todo something
            }
        }
    • 场景切换事件
        SceneManager.activeSceneChanged += OnActiveSceneChanged;
        SceneManager.sceneLoaded += OnSceneLoaded;
        void OnActiveSceneChanged(Scene a,Scene b)
        {
            Debug.Log(a);
            Debug.Log(b);
        }
        void OnSceneLoaded(Scene a, LoadSceneMode m)
        {
            Debug.Log(a);
            Debug.Log(m);
        }
    • 协程
      1. 定义:
         IEnumerator 方法名()
         {
             yield return new WaitForSeconds(time);  // 等待 3秒 后,执行下面的方法
             // todo something
         }
      2. 使用:
        StartCoroutine(方法名)  // 开启协程
        StopCoroutine(方法名)  // 停止协程
        StopAllCoroutines()  // 停止所有协程
  17. unity 数组的使用:

    List<List<int>> list = new List<List<int>>();
    List<int> a = new List<int> { 1 };
    List<int> b = new List<int> { 2 };
    list.Add(a);
    list.Add(b);
    foreach(List<int> i in list)
    {
        foreach(int j in i)
        {
            Debug.Log(j);
        }
    }
    // 输出
    1
    2
  18. unity 字典的使用:

    Dictionary<string, Dictionary<string, string>> dict = new Dictionary<string, Dictionary<string, string>>();
    Dictionary<string, string> a = new Dictionary<string, string>(){ {"keya", "valuea"} };
    Dictionary<string, string> b = new Dictionary<string, string>(){ {"keyb", "valueb"} };
    dict.Add("a", a);
    dict.Add("b", b);
    foreach(KeyValuePair<string,Dictionary<string,string>> kp in dict)
    {
        foreach(KeyValuePair<string,string>kp1  in kp.Value)
        {
            Debug.Log(kp1.Key);
            Debug.Log(kp1.Value);
        }
    }
    // 输出
    keya
    valuea
    keyb
    valueb
文档更新时间: 2024-05-10 10:01   作者:lee