Unity2019.3API教程(二)Object类

Object类

Unity目前有三个命名空间,UnityEngine、UnityEditor、Unity,当然还有一个Other是做补充说明的,Object类是一个基类,可以查阅本教程第一篇文档的图片,做直观了解,下面详细解析。

1. 官方定义
class in UnityEngine/Implemented in:UnityEngine.CoreModule
属于UnityEngine命名空间下的核心模块类

2. 官方描述

Base class for all objects Unity can reference.
Any public variable you make that derives from Object gets shown in the inspector as a drop target,allowing you to set the value from the GUI. UnityEngine.Object is the base class of all built-in Unity objects.
Although Object is a class it is not intended to be used widely in script. However as an exampleObject is used in the Resources class. See Resources.LoadAll which has [[Object[]]] as areturn.
This class doesn’t support the null-conditional operator (?.) and the null-coalescing operator (??).

上述说明‎Unity 可以引用所有对象的基类。
Object类的派生类定义的公共(public)变量可以在inspector面板中显示出来,并且设置相关的值,UnityEngine.object 是所有内置 Unity 对象的基类。‎
Object类不推荐在脚本中广泛使用,但可作为示例对象在Resources类中使用,实际开发过程中是基本不会直接用的。在Resources类中使用的情况,我们后期根据Resources.LoadAll文档进行说明,此处不做赘述。
‎Object类不支持 null 条件运算符 (?. ) 和空合并运算符 (?? )

3. 属性
hideFlags:Should the object be hidden, saved with the Scene or modifiable by the user?
hideFlag属性用于对象隐藏,场景保存和修改,具体使用我们在HideFlags类中进行叙述,这里也不做实例演示。

name:The name of the object.
语法: public string name;
该属性十分简单,直接赋值即可,演示如下:

using UnityEngine;

public class nameTest:MonoBehaviour
{
   public GameObject go;
   void Start()
       {
         go.name="icestring";
         Debug.Log("The name is"+go.name);
       }
}

4. 共有方法
GetInstanceID:Returns the instance id of the object.
语法:public int GetInstanceID();
Unity中每个物体都有自己的ID,可通过该方法获取对象ID,演示如下:

using UnityEngine;

public class IDTest : MonoBehaviour
{
   void Start()
   {
       GameObject go=new GameObject();
       Debug.Log(go.GetInstanceID());
   }

}
    

ToString:Returns the name of the object.

语法:public string ToString();
该方法将返回对象的名称,演示如下:

using UnityEngine;

public class TSTest : MonoBehaviour
{
    GameObject go=new GameObject();

    void Start()
    {
       Debug.Log(go.ToString());
    }
}

5. 静态方法
Destroy:Removes a GameObject, component or asset.
语法:public static void Destroy(Object obj,float t = 0.0F);
‎删除游戏对象、组件或资源。‎有两个形参,一个对象一个时间,时间为可选项。‎对象在当前 Update 循环后下一帧渲染之前立即销毁, 如果指定时间,则从现在开始几秒钟销毁该对象。如果是‎‎组件‎‎,此方法将从‎‎GameObject‎‎中删除组件并销毁它。演示如下:

using UnityEngine;

public class DTest: MonoBehaviour
{
     GameObject go=new GameObject();
     void Update()
     {
         Destroy(go,2.0f);
     }
}
  DestroyImmediate:Destroys the object obj immediately. You are strongly recommended to use Destroy instead.

语法:public static void DestroyImmediate(Object obj,bool allowDestroyingAssets = false);
此方法可以立即销毁对象,含有两个参数,对象和一个bool值,bool值决定是否立即销毁,该方法风险很高,可能会将资源删除,需谨慎使用。演示如下:

using UnityEngine;

public class DITest : MonoBehaviour
{
         GameObject go=new GameObject();
         void Update()
         {
            DestroyImmediate(go,true);
         }
}
  DontDestroyOnLoad:Do not destroy the target Object when loading a new Scene.

语法:public static void DontDestroyOnLoad(Object target);
该方法允许在加载新场景时不销毁一些物体,含有一个参数,指定该对象不被销毁,因涉及到场景切换,会使用SceneManagement类,我们在SceneManagement类中再讨论该方法,本篇不做代码演示。

  FindObjectOfType:Returns the first active loaded object of Type type.
  语法:public static Object FindObjectOfType(Type type);

该方法通过类型查找单个物体,只含有一个参数,用于返回类型,演示如下。

using UnityEngine;
using System.Collections;

public class FGTest : MonoBehaviour
{
    void Start()
    {
        Camera cam = (Camera)FindObjectOfType(typeof(Camera));
        if (cam)
            Debug.Log("Camera object found: " + cam.name);
        else
            Debug.Log("No Camera object could be found");
    }
}
  FindObjectsOfType:Returns a list of all active loaded objects of Type type.
  语法:public static T[] FindObjectsOfType();
             public static Object[] FindObjectsOfType(Type type);
             该方法有两种写法,通过类型查找一组物体,返回的是一个数组,可以是一个泛型数组或者是Object类数组,演示如下:
using UnityEngine;

public class FGSTest : MonoBehaviour
{
    private const int count = 10;

    void Start()
    {
        var gameObjects = new GameObject[count];
        var expectedTextMeshObjects = new TextMesh[count];
        var expectedCanvasObjects = new CanvasRenderer[count];

        for (var i = 0; i < count; ++i)
        {
            gameObjects[i] = new GameObject();
            expectedTextMeshObjects[i] = gameObjects[i].AddComponent<TextMesh>();
            expectedCanvasObjects[i] = gameObjects[i].AddComponent<CanvasRenderer>();
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var foundCanvasObjects = FindObjectsOfType<CanvasRenderer>();
            var foundTextMeshObjects = FindObjectsOfType(typeof(TextMesh));
            Debug.Log(foundCanvasObjects + " : " + foundCanvasObjects.Length);
            Debug.Log(foundTextMeshObjects + " : " + foundTextMeshObjects.Length);
        }
    }
}

以上代码讲述了,先定义三个容量为10的不同类型数组,然后在for循环里gameObjects 实例化,expectedTextMeshObjects和expectedCanvasObjects获取到对应组件,最后在Update函数里通过ifxuanze结构进行寻找并通过Debug.Log方法进行打印,没有输出gameObjects的原因是组件时挂载在gameObjects上的,控制台打印时也应用了两种类型的写法。

  Instantiate:Clones the object original and returns the clone.
  语法:
public static Object Instantiate(Object original);
public static Object Instantiate(Object original,Transform parent);
public static Object Instantiate(Object original,Transform parent,bool instantiateInWorldSpace);
public static Object Instantiate(Object original,Vector3 position,Quaternion rotation);
public static Object Instantiate(Object original,Vector3 position,Quaternion rotation,Transform parent);
Instantiate函数是unity3d中进行实例化的函数,也就是对一个对象进行复制操作的函数,这个函数共有五个重载(overloaded)函数,参数解释如下:

original:源对象
position:位置坐标
rotation:旋转坐标(旋转四元数)
parent:实例化对象的父对象,就是给这个实例化对象找的爹,在完成实例化函数处理后,实例化对象将在父对象下,是父对象的子对象
instantiateWorldSpace:这个值为True,表示实例化对象相对于世界坐标系的位置(是位置,不是坐标值,比如实例化前在Canvas左上角,实例化后还在左上角)不变,相对于父对象的坐标值变了。为false,表示实例化对象相对于父对象的坐标值不变,但在世界坐标系中的位置变了。
演示如下:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform prefab;
    void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
        }
    }
}

因为实例化的函数可以重载,在这里只给出一种写法,避免赘述,其实并不难,调用时根据实际需求更改函数的参数即可。

6. 运算符
bool:Does the object exist?
operator !=:Compares if two objects refer to a different object.
operator ==:Compares two object references to see if they refer to the same object.
以上三个运算符很简单,分别是bool值,不等于和等于,用法上,bool表示判断对象石佛营存在,!=表示‎比较两个对象是否引用其他对象,==表示比较两个对象引用以查看它们是否引用同一对象。‎

Object类的解析就到这里,有些地方可能说的不是十分详细,其实不用太过于纠结,知道方法和属性是做什么的,拿着用就可以了,如果C#语言基础扎实的话,代码应该都是看的懂的,有些代码我参考了官方文档,过于复杂的我会给解释。

你可能感兴趣的:(笔记)