Unity

UnityAPI

1\ 编辑器

    2017之前版本 : MonoDevelop
    2017之后  : VS
    修改使用编辑器 : 菜单栏Editor=》Preferences=》External Tools=》External Script Editor 

2\ 编辑器模版修改

  路径 : D:\***\Editor\Data\Resources\ScriptTemplates

3\ MonoBehaviour类

  继承这个类可以使脚本可以像组件一样挂在物体上.
  脚本无法挂在物体上:是否继承MonoBehaviour类;检查脚本名字和类名是否相同.
  协程也在这个类里边.

4\ Debug类


Debug.Log("控制台输出 信息");
Debug.LogWarning("控制台输出 警告 信息");
Debug.LogError("控制台输出 错误 信息");
print("print输出");//控制台输出

Debug.Log与Print区别
  print : print  是MonoBehaviour的一个成员,print  必须要继承  MonoBehaviour  类,
  Debug.Log: Debug则是一个独立的、密闭的类,Debug不需要继承  MonoBehaviour  类,
//print与Debug.Log的区别?
//print继承MonoBehaviour
//Debug类是一个单独的类

画线
Debug.DrawLine(vStart, vEnd, Color.red);
Debug.DrawRay(vStart, vEnd, Color.red);
上面两个方法都是用来画线,只能在场景视图中看见,游戏视图中看不见

5\ Transform类


    Transform组件
     作用 :
           ① 负责游戏对象的变换
                位置
                旋转
                缩放
            ② 维持父子关系 : 子物体的位置、旋转角度、缩放会随着父物体的改变而改变
          在Hierarchy视图中,每一个Transform可以有一个父级,允许分层次管理位、旋转和缩放。

6\脚本之间的调用

    _Data1 :
         public string name;
    void Awake()
    {
        name = "战三";
    }
    _Data2 :
        public _Data1 data1;
    void Start()
    {
        data1 = transform.GetComponent<_Data1>();//获取脚本
        Debug.Log(data1.name);//调用字段
    }

7\SendMessage参数传递


  BroadcastMessage()方法:可以调用指定物体本身和子物体上的方法
         //gameObject.BroadcastMessage("ApplyMessage"  //调用的方法名
        //                            , "传递的参数"//传递的参数
        //                            , SendMessageOptions.DontRequireReceiver);//消息发送方式
        /*
           SendMessageOptions消息发送方式选项
                RequireReceiver    需要接收方。//如果没有接受方会报错
                DontRequireReceiver    无需接收方。
        */


    SendMessage()方法:可以调用指定物体上的方法
        //(父物体、子物体中所有MonoBehavior都不会接收到该消息,
        //即使包含methodName的方法)
        gameObject.SendMessage("ApplyMessage"
                                , gameObject.name
                                , SendMessageOptions.RequireReceiver);


    SendMessageUpwards()方法:可以调用指定物体和其父物体上的方法
        //注意:B物体可以调用父物体A上方法,但是调用不到父物体中子物体C同级的方法
        //发送对象为该游戏对象及其所有父对象的所有MonoBehavior。
        //(即使子物体中某些Monobehavior包含该消息,也不会执行~)
        gameObject.SendMessageUpwards("ApplyMessage"
                                        , gameObject.name
                                        , SendMessageOptions.RequireReceiver);

8\ Time类

在Unity中封装了一个Time类,Time类是从Unity获取时间信息的接口。

9、延迟执行

 Invoke("MyDebug", 3.0f);//延迟多少秒执行(注意程序不会暂停)

10、协程

 计时器:
    bool isOn;//表示计时器状态
    private void Update()
    {
        if (isOn == false)//计时器没有开启
        {
            StartCoroutine("TimeJSQ");
            isOn = true;//表示计时器开启
        }
    }
    IEnumerator TimeJSQ()
    {
        yield return new WaitForSeconds(3);
        Debug.Log("计时器协程写法");
        isOn = false;
    }

11、Rigidbody类

AddForce : 
        if(Input.GetMouseButtonDown(0))
        {
            rig.AddForce(0, 500, 0);//刚体增加力
        }

12、Random类

Range():
        Random.Range(0,10);//第二个数为整数:能取到0 取不到10       [0,1)
Random.Range(0, 10.0f);  //第二个数为浮点数:能取到0 取到10  [0,1.0f]

13、Application类

  退出项目: Application.Quit();
  跳转场景(已弃用):Application.LoadLevel("WWW");Application.LoadLevel(1);
  打开链接方法: Application.OpenURL("https://www.baidu.com/");
  四种不同的path 都为只读
   a. 输出当前项目的Assets目录: Application.dataPath
   b. Assets目录下StreamingAssets文件夹目录: Application.streamingAssetsPath
   c.  persistentDataPath和temporaryCachePath的路径位置一般是相对所在系统的固定位置
:Application.persistentDataPath(可读可写)
  d.子主题 4 : Application.temporaryCachePath

14、SceneManager类

跳转场景
SceneManager.LoadScene("WWW");
SceneManager.LoadScene(1);

获取场景名称
SceneManager.GetActiveScene().name
Debug.Log("返回场景的名称:" + SceneManager.GetActiveScene().name);

获取场景索引
GetActiveScene().buildIndex
Debug.Log("返回 Build Settings 中场景的索引:" + SceneManager.GetActiveScene());
注意一定要把场景加入到Build Settings中,不然获取的是空值

15、Quaternion类

Euler()方法
        Euler通过欧拉角创建一个四元数:主要用来赋值旋转角度
        transform.rotation = Quaternion.Euler(30,30,30);

identity属性
        Quaternion.identity:单位旋转,相当于没有旋转

16、ray类

    
  ·1     

        Vector3 vStart = transform.position;
        Vector3 vEnd = vStart + new Vector3(0, 0, 100);
        Ray ray = new Ray(vStart, vEnd);
        Debug.DrawLine(vStart, vEnd, Color.red);//画出射线
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log("名字:" + hit.collider.name);//射线碰撞物体的名字
            Debug.Log("碰撞坐标:" + hit.point);//射线碰撞的坐标
            Debug.Log("法线:" + hit.normal);//射线命中的表面的法线
            Debug.Log("距离:" + hit.distance);//距离
            Debug.Log("碰撞物体:" + hit.collider.gameObject);//获取碰撞物体
        }
    2
         if (Input.GetMouseButtonDown(0))//鼠标按下时发射摄像
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))//如果发生碰撞,返回碰撞信息hit
            {
                Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
                Debug.Log(hit.collider.name);//射线碰撞物体的名字
            }
        }
    3
        //射线另一种写法
        Ray ray = new Ray(transform.position, transform.forward);
        Debug.DrawLine(transform.position, transform.forward * 100, Color.blue);
        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        //if (Physics.Raycast(ray, out hit, 100, ~512))//100是射线的长度
        if (Physics.Raycast(ray, out hit, 100, ~(1 << 5)))//100是射线的长度
        {
            Debug.Log(hit.collider.name);
        }
        //0000000000000000000000000000011
        //必须加上最大长度
        //(1 << layerNumber)只检测某一层(layerNumber表示layer层的标号)
        //~(1 << layerNumber)不检测某一层,其余都检测
        //(1 << layerNumber)|(1 << layer2Number)只检测某2个层
    4
        if (Input.GetMouseButtonDown(0))
        {
            //射线另一种写法
            Ray ray = new Ray(transform.position, transform.forward);
            Debug.DrawLine(transform.position, transform.forward * 100, Color.blue);
            RaycastHit[] hits;
            //返回所有碰撞信息
            hits = Physics.RaycastAll(ray);
            //增加射线碰撞距离
            hits = Physics.RaycastAll(ray, 100);
            //增加检测层
            hits = Physics.RaycastAll(ray, 100, 1 << 10);

            //输出所有物体碰撞信息
            foreach (var item in hits)
            {
                Debug.Log(item.collider.name);
            }
        }
    Ray 参加碰撞检测的射线
    RaycastHit  射线的碰撞信息
    maxDistance  射线的长度
    layerMask 射线遮罩:检测哪一层;不检测哪一层

17、Screen类

    当前的分辨率  Screen.currentResolution
    全屏设置  Screen.fullScreen = true;
    屏幕窗口的当前高度(以像素为单位) Screen.height
    屏幕窗口的当前高度(以像素为单位) Screen.width
    切换屏幕分辨率    Screen.SetResolution(800,400,true);
    屏幕自适应:(开发中)当前屏幕高/当前屏幕宽=发布后屏幕高/发布后的屏幕宽

18、Mathf类

用来计算
    Debug.Log("圆周率" + Mathf.PI);
        //绝对值
        Debug.Log(Mathf.Abs(-10.5f));
        //返回最大值
        Debug.Log(Mathf.Max(1.2f, 2.4f));
        //返回次幂
        Debug.Log(Mathf.Pow(6, 1.8f));
        //返回平方根
        Debug.Log(Mathf.Sqrt(3 * 3 + 4 * 4));

19、Gizmos类

Gizmos 菜单中方法
    public Transform target;
    void OnDrawGizmosSelected()
    {
        if (target != null)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(transform.position, target.position);
        }
    }

  编辑器未运行,方法时时运行

20、协同


    定义协程
        IEnumerator+方法名
        yield return+.....

调用协程
        字符串:StartCoroutine("MyFun");//调用协程
        方法
            StartCoroutine(MyFun());//调用协程
            注意:StopCoroutine(MyFun());//终止不了协程

终止协程
        StopCoroutine("MyFun");//终止协程
终止所有协程
        StopAllCoroutines();//终止所有协程
等待时间
        yield return new WaitForSeconds(5);//5秒以后执行


    另一种写法
        //另一种写法
        IEnumerator myFun;//定义一个协程变量
        myFun = MyFun();//赋值方法
        StartCoroutine(myFun);
        StopCoroutine(myFun);
IEnumerator MyFun()
    {
        Debug.Log("111111协同被调用0");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("111111协同被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("111111协同被调用2");
    }


    注意
StartCoroutine("MyFun");//调用协程
StartCoroutine("MyFun2");//调用协程  
MyFun:正在执行中,开启另一个MyFun2
MyFun2
实例
void Start()
    {
        StartCoroutine("MyFun");//调用协程
        StartCoroutine("MyFun2");//调用协程
    }
    IEnumerator MyFun()
    {
        Debug.Log("MyFun调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun被调用2");
    }
    IEnumerator MyFun2()
    {
        Debug.Log("MyFun2被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用2");
    }


yield return MyFun();
yield return MyFun2();
MyFun()执行完执行MyFun2()
实例
void Start()
    {
        StartCoroutine("MyFun");//调用协程
    }
    IEnumerator MyFun()
    {
        yield return MyFun2();
        yield return MyFun3();
    }
    IEnumerator MyFun2()
    {
        Debug.Log("MyFun2被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun2被调用2");
    }
    IEnumerator MyFun3()
    {
        Debug.Log("MyFun3被调用");
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun3被调用1");
        yield return null;
        yield return new WaitForSeconds(5);//5秒以后执行
        Debug.Log("MyFun3被调用2");
    }

21、场景中的鼠标事件

  鼠标按下
         private void OnMouseDown()
    {
        Debug.Log("鼠标按下");
    }
    鼠标抬起
         void OnMouseUp()
    {
        Debug.Log("鼠标抬起");
    }
    鼠标在按下时拖动
        private void OnMouseDrag()
    {
        Debug.Log("鼠标按下时拖动");
    }
    鼠标进入
         private void OnMouseEnter()
    {
        Debug.Log("鼠标进入");
    }
    鼠标离开
         void OnMouseExit()
    {
        Debug.Log("鼠标离开");
    }
    鼠标经过
        void OnMouseOver()
    {
        Debug.Log("鼠标经过");
    }
    鼠标按下抬起时调用
        void OnMouseUpAsButton()
    {
        Debug.Log("鼠标按下抬起时调用");//单击
    }

    Vector类
    定义一个向量:Vector3 v3 = new Vector3(0,0,0);
    简写
 Debug.Log("Vector3.up:"+Vector3.up);//0 1 0                Debug.Log("Vector3.down:"+Vector3.down);//0 -1 0      Debug.Log("Vector3.left:"+Vector3.left);//-1 0 0                     Debug.Log("Vector3.right:"+Vector3.right);//1 0 0     Debug.Log("Vector3.forward:"+Vector3.forward);// 0 0 1     Debug.Log("Vector3.back:"+Vector3.back);//0 0 -1         Debug.Log("Vector3.one:"+Vector3.one);//1 1 1         Debug.Log("Vector3.zero:"+Vector3.zero);//0 0 0
z轴方向,是他的正方向

 返回向量长度:
 Vector3 v1 = new Vector3(1,1,1);                                                                                             Vector3 v2 = new Vector3(2,2,2);                                                                                                       float f1 = Vector3.Magnitude(v2-v1);            //float f2 = Vector3.Magnitude(v1-v2);                         Debug.Log("返回向量长度" + f1);

返回向量长度的平方:
 Vector3 v11 = new Vector3(1,1,1);                                                                                                 Vector3 v22 = new Vector3(2,2,2);                                                                                                     float f3 = (v11-v22).sqrMagnitude;

二、数据持久化

1、Resources类

允许您查找和访问资源等对象。
  img = Resources.Load("body_02") as Texture;
  img = Resources.Load("01/body_03") as Texture;
  Sphere = Resources.Load("Sphere") as GameObject;
 注意:Resources文件夹里存放的资源不能超过2g
            把资源加载到内存并不在场景里,如果需要使用,需要实例化到场景中

2、玩家偏好

存储小型数据 ,把数据存在注册表中
存:
        PlayerPrefs.SetFloat("01", 15.5f);
        PlayerPrefs.SetInt("ing", 14);
        PlayerPrefs.SetString("name", "zhangsan");
取:
        Debug.Log(PlayerPrefs.GetFloat("01"));
        Debug.Log(PlayerPrefs.GetInt("ing"));
        Debug.Log(PlayerPrefs.GetString("name"));
  //注意
          PlayerPrefs.SetInt("name", 3);
        PlayerPrefs.SetFloat("name", 3.3f);
        PlayerPrefs.SetString("name", "zhangsan");
        PlayerPrefs.GetInt("name");
        PlayerPrefs.GetFloat("name");
        PlayerPrefs.GetString("name");
   只有    PlayerPrefs.GetString("name");有值

删除所有键和值 :PlayerPrefs.DeleteAll(); //请谨慎使用。

从偏好中删除 key 及其对应值 :PlayerPrefs.DeleteKey("name");

有没有键,返回true或false: PlayerPrefs.HasKey("name");

保存 :PlayerPrefs.Save();

属于与玩家偏好结合使用
    public string Name
    {
        set { PlayerPrefs.SetString("name",value); }
        get { return PlayerPrefs.GetString("name"); }
    }
    void Start()
    {
        //存
        Name = "张三";
        //取
        Debug.Log(Name);
;    }

3、Vector类

定义一个向量 : Vector3 v3 = new Vector3(0,0,0);
简写:
        Debug.Log("Vector3.up:" + Vector3.up);// 0  1  0
        Debug.Log("Vector3.down:" + Vector3.down);//0  -1  0
        Debug.Log("Vector3.left:" + Vector3.left);//-1  0  0
        Debug.Log("Vector3.right:" + Vector3.right);//1  0  0
        Debug.Log("Vector3.forward:" + Vector3.forward);//0 0 1
        Debug.Log("Vector3.back:" + Vector3.back);//0 0 -1
        Debug.Log("Vector3.one:" + Vector3.one);//1 1 1
        Debug.Log("Vector3.zero:" + Vector3.zero);//0 0 0

返回向量长度
        Vector3 v1 = new Vector3(1, 1, 1);
        Vector3 v2 = new Vector3(2, 2, 2);
        float f1 = Vector3.Magnitude(v2 - v1);
        //float f2 = Vector3.Magnitude(v1 - v2);
        Debug.Log("返回向量长度" + f1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值