好记性不如烂笔头=。=

场景

  1. 脚本生命周期: ResetAwakeOnEnableStartFixedUpdate(0.02s)、UpdateLateUpdateOnGUIOnDisableOnDestroy

  2. 添加爆炸力(1000大小,半径10米):AddExplosionForce(1000,transform.position,10);

  3. 碰撞检测: OnCollisionEnter(Collision collision) OnCollisionStay OnCollisionExit
    触发检测:OnTriggerEnter(Collider other) OnTriggerStay OnTriggerExit

  4. 重力感应:Input.acceleration
    鼠标进入、停留、离开
    3D物体:OnMouseEnterOnMouseStayOnMouseExit
    UI:using UnityEngine.EventSystems 接口IPointerEnterHandler-public void OnPointerEnter(PointerEventData eventData)IPointerExitHandler-public void OnPointerExit(PointerEventData eventData)
    滚轮滑动(望远镜):Input.GetAxis("Mouse ScrollWheel")

  5. 异步加载场景:(使用协程)

AsyncOperation ao;
ao = SceneManager.LoadSceneAsync("场景二");
yield return ao;
  1. 合并路径:System.IO.Path.Combine
  2. 开始5s后调用,每2s调用一次:InvokeRepeating("A",5,2);
  3. 但摄像机不可见时:On BecameInvisible
  4. 读取txtstring str = Resources.Load("txt1").ToString();
    用,分开一串字符:string[] words = str.Split(',');

10.删除

List a = new List();
    void Start ()
    {
        foreach (Transform item in transform)
        {
            if (item.name.Equals("Cube"))
            {
                a.Add(item);
            }
        }
        for (int i = 0; i < a.Count; i++)
        {
            Destroy(a[i].gameObject);
        }
    }

11.面板限制字段取值范围:

[Range(0,10)]
int a;

14.动态更换天空盒:RenderSettings.skybox = newSkybox;
15计时器

 time_Line += Time.deltaTime;
 int hour = (int)time_Line / 3600;
 int minute = ((int)time_Line - hour * 3600) / 60;
 int second = (int)time_Line - hour * 3600 - minute * 60;
 int millisecond = (int)((time_Line - (int)time_Line) * 1000);
 Tex_Linetime.text = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", hour, minute, second, millisecond);

16.text = string.Format("{0:d2}:{1:d2}:{2:d2}", shi, fen, miao);

18.当前时间:string path = Application.dataPath + string.Format("/Photos/Photo_")+ System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
19.划线

public void drawLine()
    {
        if (Input.GetMouseButton(0))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                    lineRenender.positionCount = index + 1;
                    lineRenender.SetPosition(index, hit.point));
                    index++;
            }
        }
    }

20、Toggle注册
Tog_XiaDun.onValueChanged.AddListener(ison => clickTog(ison));

void clickTog(bool ison)
    {
        if (ison)
        {
             DeBug.Log(true);
        }
        else
        {
            DeBug.Log(false);
        }
    }

UI

  1. 更换Image的Sprite:bg.sprite = Resources.Load("bg",typeof(Sprite)) as Sprite;
  2. 隐藏鼠标: Cursor.visible= false;
    跟换鼠标指针
public Texture2D cursora;
public CursorMode cm = CursorMode.Auto;//渲染形式,auto为平台自适应显示
Cursor.SetCursor(cursora, Vector2.zero, cm);
3.改变RectTransform大小
RectTransform.sizeDelta = new Vector2(rT.sizeDelta.x,rT.sizeDelta.y);

3.Texture2D转Sprite
img.sprite = Sprite.Create(texture,new Rect(0, 0, texture.width, texture.height), Vector2.zero);

数学

  1. 钳制:a = Mathf.Clamp(a,0,MAX),如果a小于0,返回0;如果a大于MAX,返回MAX。
  2. 小数点后保留两位:((float)Mathf.Round(Num*100))/100 stringText:.ToString("N2")
  3. 随机数:Unity:UnityEngine.Random.Range(0,10)、C#:System.Random rand =new System.Random; rand.Next(10)
  4. 四元数、欧拉角:
    1. Euler To Quaternion
    2.Quaternion To Euler
    3.AngleAxis To Quaternion
    4.Quaternion To AngleAxis
  5. Mathf.lerp 插值运动 Mathf.MoveTowards 匀速运动
    Mathf.PingPong 乒乓运动
    Cube.position =new Vector3(Mathf.PingPong(Time.time * speed) , 10),0,0)

动画

  1. 判断动画播放结束:
AnimatorStateInfo asi = anim.GetCurrentAnimatorStateInfo (0);
if (animatorInfo.normalizedTime > 1.0f)//normalizedTime: 范围0 -- 1,  0是动作开始,1是动作结束
{
    //播放结束
}

11.dotween动画连续播放:
1、Tweener tw1,Tweener tw2
Sequence se1 = DOTween.Sequence().Append(tw1).Append(tw2);
2、tw.OnComplete(() => { Fun(i); });
17.iTween.MoveTo(cam, iTween.Hash("x", -150, "y", 785, "z", 0, "time", 4, "delay", 0));

VR-AR

  1. VR项目中,射线点到UI按钮上闪烁不灵敏:是因为在VR项目中UI是在世界坐标下,按钮跟背景在同一水平面上,所以要将按钮往前移动一像素。

WEB

1.Unity发布Html网页端的调用方法

    

存储

不同平台读取StreamingAssets下的文件路径

string filePath =
#if UNITY_ANDROID && !UNITY_EDITOR
        "jar:file://" + Application.dataPath + "!/assets/" + "name"; 
#elif UNITY_IPHONE && !UNITY_EDITOR
        Application.dataPath + "/Raw/" + "name"; 
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
        "file://" + Application.dataPath + "/StreamingAssets" + "/" + "name";
#endif

移动端

移动端单指偏移量
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

你可能感兴趣的:(好记性不如烂笔头=。=)