从下面这张大佬做的图,
我们知道GameObject类和Component都继承于一个Object类,而组件类又派生出许多种组件。
而组件实例化,必须添加到游戏对象中。
游戏对象的许多属性和特征都要由组件表现。
即Component类实例化的对象,必须依附GameObject类的对象而存在。
---------------------------------------------------------------
组件的类方法,也是通过GameObject类的对象来体现出现了
这张图是从http://blog.csdn.net/duzixi来的
Component属性
Component.gameObject【组件-对象】
------------------------------------------------------
public GameObject gameObject;
------------------------------------------------------
//Component如何实现与GameObject的关联
一般情况下,定义了一个"GameObject对象"的同时,
也定义了一个"Component对象"。
而每定义一个"Component对象"又会创建一个没有Component组件的"GameObject的对象",
名字为"gameObject"。
名字为"gameObject"的纯空对象,
和最开始的"GameObject对象"通过引用的方式,
使他们指向同一个对象。
这样就轻轻松松的实现了组件和对象的关联了。
//向对象添加组件的方式是一样的
这也是为什么我们可以在脚本内通过"gameObject"这个名称,调用到脚本挂载到的对象。
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
print(gameObject.name);
}
}
------------------------------------------------------
Component.tag【组件标签】
------------------------------------------------------
public string tag;//标签的定义,本质是字符串
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
Debug.Log("组件的标签: " + gameObject.tag);
//从这个例子可以看出,GameObject类实例化对象的标签
//就是组件的标签
}
}
Component.name【对象层级名】
------------------------------------------------------
public string name;//层级名的定义,本质是字符串
------------------------------------------------------
Component【组件名】
------------------------------------------------------
Debug.Log(组件对象名); //这个返回的是组件在检查器中的名字
------------------------------------------------------
Component.transform【位置组件】
------------------------------------------------------
public Transform transform; //定义一个位置组件
------------------------------------------------------
"Transform"类是一个"Component"组件派生类,是一个"组件类"
//那么为什么要把一个位置组件归为组件属性的一部分?
是因为这玩意太重要了,每一个游戏对象在场景中都要有一个明确的位置,
不然这个组件该如何出现在场景中。
所以,虽然Transform类是一个组件类,但也归为组件和对象的基本属性。
//由于是一个专门位置的组件,这里就不专门细分开讲了
------------------------------------------------------
//unity官方例子
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
transform.Translate(1, 1, 1);
}
}
公有类方法-(PublicMethods)
其实这些方法GameObject类里都有,但是我偷懒没仔细看。
Component.SendMessage消息传递类方法
Component.BroadcastMessage 自己及其子对象收得到
Component.SendMessage 仅自己收得到
Component.SendMessageUpwards 自己及其父级对象收到的
------------------
"注意:"未激活对象收不大,没有子父关系的对象收不到
Component.BroadcastMessage【广播消息】:自己及其子对象收得到
------------------------------------------------------
//函数原型
public void BroadcastMessage(
string methodName,
object parameter = null,
SendMessageOptions options = SendMessageOptions.RequireReceiver
);
public void BroadcastMessage(string methodName, SendMessageOptions options);
------------------------------------------------------
//参数说明
"methodName":需要调用的函数名(用字符串表示)
"parameter" :发送的参数值【可选参数】
"options" :如果有对象没有接手到消息是否报错【可选参数】
//SendMessageOptions.RequireReceiver //这个参数表示返回(默认是返回的)
//SendMessageOptions.DontRequireReceiver//这个表示不返回
------------------------------------------------------
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
BroadcastMessage("ApplyDamage", 5.0 ,SendMessageOptions.DontRequireReceiver);
//调用所有(自身、子)对象中名为"ApplyDamage"的函数,并接受5.0这个参数
//SendMessageOptions.DontRequireReceiver表示如果对象没有这个函数,
//不返回错误信息
}
//这个函数可以放在任意一个子对象中,用于接收消息。
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.SendMessage【发送消息】仅自己收得到
------------------------------------------------------
//函数原型
public void SendMessage(string methodName);
public void SendMessage(string methodName, object value);
public void SendMessage(string methodName, object value, SendMessageOptions options);
public void SendMessage(string methodName, SendMessageOptions options);
------------------------------------------------------
//参数说明
"methodName":需要调用的函数名(用字符串表示)
"value" :发送的参数值【可选参数】
"options" :如果有对象没有接手到消息是否报错【可选参数】
//SendMessageOptions.RequireReceiver //这个参数表示返回(默认是返回的)
//SendMessageOptions.DontRequireReceiver//这个表示不返回
------------------------------------------------------
//使用方法和上面那个完全一样,只是发送的对象不一样。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
SendMessage("ApplyDamage", 5.0);
}
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.SendMessageUpwards【向上发送消息】自己及其父级对象收到的
------------------------------------------------------
//函数原型
public void SendMessageUpwards(string methodName, SendMessageOptions options);
public void SendMessageUpwards(
string methodName,
object value = null,
SendMessageOptions options = SendMessageOptions.RequireReceiver
);
------------------------------------------------------
//参数说明
"methodName":需要调用的函数名(用字符串表示)
"value" :发送的参数值【可选参数】
"options" :如果有对象没有接手到消息是否报错【可选参数】
//SendMessageOptions.RequireReceiver //这个参数表示返回(默认是返回的)
//SendMessageOptions.DontRequireReceiver//这个表示不返回
------------------------------------------------------
//使用方法和上面那个完全一样,只是发送的对象不一样。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
SendMessageUpwards("ApplyDamage", 5.0);
}
void ApplyDamage(float damage)
{
print(damage);
}
}
Component.CompareTag标签
Component.CompareTag
------------------------------------------------------
//函数原型
public bool CompareTag(string tag);
------------------------------------------------------
//参数说明
返回值 :bool (布尔值)
string tag:接受一串字符串
------------------------------------------------------
//官方例子
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
}
Component.GetComponent获取组件
Component.GetComponent【获取组件】
------------------------------------------------------
//函数原型
public Component GetComponent(Type type);
public T GetComponent();
public Component GetComponent(string type);
------------------------------------------------------
//参数说明
返回值:返回组件
Type type :typeof(组件类型) //这个参数要接受一个函数,表示组件类型
------------------------------------------------------
//官方示例
using UnityEngine;
public class ScriptExample : MonoBehaviour
{
void Start()
{
Transform xyz = GetComponent<Transform>();
Transform xyz2 = GetComponent("Transform") as Transform;
//这两个都一样
}
}
Component.GetComponents【获取当前对象所有的组件】
------------------------------------------------------
//函数原型
public Component[] GetComponents(Type type);
public T[] GetComponents();
------------------------------------------------------
//参数说明
"返回值" :Component[] //返回组件组
Type type : typeof(组件类型) //这个参数要接受一个函数,表示组件类型
------------------------------------------------------
"功能" :返回所有类型为Type的组件
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Component[] comp = gameObject.GetComponents<Component>();
for (int i = 1; i < comp.Length; i++)
{
Debug.Log("\n层级名:" + gameObject.name);
Debug.Log("\n组件名称:" + comp[i]); //返回组件的名字
}
}
}
Component.GetComponentInChildren
------------------------------------------------------
//函数原型
public Component GetComponentInChildren(Type t);
------------------------------------------------------
//
这个函数感觉有点迷,按官方的文档上说,应该以深度搜索然后返回子对象的组件。
但我在实际测试中,效果却和GetComponent一样。
//应该是我没搞懂这个怎么用
Component.GetComponentInParent
------------------------------------------------------
//函数原型
public Component GetComponentInParent(Type t);
------------------------------------------------------
//这个类方法,官方连示例都懒得写了
但经过测试,这个函数也和上面那个类方法一样迷
Component.GetComponentsInParent【返回自己和父级的全部组件】
------------------------------------------------------
public Component[] GetComponentsInParent(Type t, bool includeInactive = false);
public T[] GetComponentsInParent(bool includeInactive);
public T[] GetComponentsInParent();
------------------------------------------------------
//参数说明
返回值 :返回所有自己及其子级组件。
bool includeInactive:
是否返回未激活的组件。
//true :返回
//false:不返回
Type t :typeof(组件类型) //这个参数要接受一个函数,表示组件类型
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
//这两句是一样的
//Component[] comp = GetComponentsInParent(typeof(Component),false);
Component[] comp = GetComponentsInParent<Component>();
for(int i = 0; i < comp.Length; i++)
{
Debug.Log("\n层级名:" + (comp[i]).name);
Debug.Log("\n组件名称:" + (comp[i]));
}
}
}
------------------------------------------------------
Component.GetComponentsInChildren【返回自己和子级的全部组件】
------------------------------------------------------
public Component[] GetComponentsInChildren(Type t, bool includeInactive);
------------------------------------------------------
//参数说明
返回值 :返回所有自己及其子级组件。
bool includeInactive:
是否返回未激活的组件。
//true :返回
//false:不返回
Type t :typeof(组件类型) //这个参数要接受一个函数,表示组件类型
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
//这两句是一样的
//Component[] comp = GetComponentsInChildren(typeof(Component),false);
Component[] comp = GetComponentsInChildren<Component>();
for(int i = 0; i < comp.Length; i++)
{
Debug.Log("\n层级名:" + (comp[i]).name);
Debug.Log("\n组件名称:" + (comp[i]));
}
}
}
Component.TryGetComponent【尝试获取组件,如果组件不存在,不返回类型】
------------------------------------------------------
public bool TryGetComponent(Type type, out Component component);
public bool TryGetComponent(out T component);
------------------------------------------------------
Type type:寻找参数类型
out Comonent component :定义一个组件,并把找到的组件与其相引用
out T component:
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
if(gameObject.TryGetComponent<Transform>(out Transform xyz) == false)
{
Debug.Log("组件获取失败");
}
}
}
继承公共类方法
(继承自Object类的类方法)
Object.ToString 返回层级中的名字
Object.ToString
------------------------------------------------------
public string ToString();
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//对象名.ToString();
//可以返回对象在层级中的名字。
Debug.Log("name:" + gameObject.ToString());
}
}
Object.GetInstanceID() 返回对象ID
Object.GetInstanceID
------------------------------------------------------
public int GetInstanceID();
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//可以返回对象的ID值
Debug.Log("ID:" + gameObject.GetInstanceID());
}
}
继承静态类方法
(继承自Object类的静态类方法)
//所谓静态,就是不必通过对象实例化就可以使用的对象。
Object.Destroy
------------------------------------------------------
public static void Destroy(Object obj, float t = 0.0F);
------------------------------------------------------
//参数说明:
Object obj :删除的组件或者对象
float t = 0.0f :延时几秒后删除
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityGameOject : MonoBehaviour
{
void Start()
{
//删除组件
Destroy(gameObject.GetComponent<BoxCollider>());
Destroy(gameObject); //直接删除对象
Destroy(gOj, 10f); //延时10秒后删除
}
}
Object.DontDestroyOnLoad
------------------------------------------------------
暂时不懂,留着以后补充
按类型查找对象
Object.FindObjectOfType
------------------------------------------------------
public static Object FindObjectOfType(Type type);
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
Transform xyz = FindObjectOfType<Transform>();
//从场景中寻找 Transform组件,并返回
}
}
Object.FindObjectsOfType
------------------------------------------------------
public static Object FindObjectsOfType(Type type);
------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
Transform[] xyz = FindObjectsOfType<Transform>();
for(int i = 0 ; i < xyz.Length; i++)
{
//返回所有用于Transform组件的对象的层级名
Debug.Log("\n层级名:" + xyz[i].ToString());
}
}
}
(菜鸟的学习笔记,如果发现问题,希望各位能指正)