您的位置:首页 > 移动开发 > Unity3D

Unity编辑器拓展【C#脚本程序基础】

2018-01-31 20:45 549 查看

Unity几个核心类

Object它是Unity对象的基类,它有一些常用方法:Instantiate用来实例化Unity对象,通过Instantiate创建的物体都会被Unity管理起来。
Destroy/DestroyImmediate销毁对象,Destroy在一般运行时候使用,对象在销毁时不是立刻就回收了,DestroyImmediate一般在写编辑器时使用,资源会立刻回收的。
FindObjectOfType/FindObjectsOfType查找某种类型的对象。
https://docs.unity3d.com/ScriptReference/Object.html

GameObject
它是Unity场景中的实体,可以把GameObject拖到Project视图里面弄成Prefab文件。

https://docs.unity3d.com/ScriptReference/GameObject.html

MonoBehavior
它是功能的具体实现体,脚本继承于MonoBehavior后,每个GameObject可以挂脚本实现不同功能。

https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

C#变量和命名空间

值类型和引用类型:http://mp.blog.csdn.net/postedit/51202926
namespace:http://mp.blog.csdn.net/postedit/77951390

C#预处理指令

C#预处理指令:http://mp.blog.csdn.net/postedit/77971711

Unity的宏

UNITY_EDITOR :判定代码是在编辑器模式下执行还是在发布平台下执行
UNITY_ANDROID:判定代码运行环境是在andriod平台
UNITY_IPHONE:判定代码运行环境是在ios平台
UNITY_4_6/UNITY_4_6_4:和Unity版本相关,比如这里4_6就表示Unity4.6的版本

C#委托

C#委托:http://mp.blog.csdn.net/postedit/78037951

Lambda表达式

只要有委托参数类型的地方,就可以使用Lambda表达式。
定义形参:一个参数只写参数名就足够了,多个参数放到()中,可以添加类型,例如://定义
public class UIEventListener : MonoBehaviour
{
public delegate void VoidDelegate(GameObject go);
public UIEventListener.VoidDelegate onClick;

public static UIEventListener Get(GameObject go);
}
//使用例子
UIEventListener.Get(EquipBtn).onClick = go =>
{
//点击事件处理
};

C#异常处理

异常处理:http://mp.blog.csdn.net/postedit/78002305

C#反射

反射:http://mp.blog.csdn.net/postedit/78123815
代码演示反射的部分用法:using UnityEngine;
using System.Collections;
using System.Reflection;

public class TestReflection : MonoBehaviour {

void OnGUI()
{
if(GUI.Button(new Rect(50,50,200,50), "Display Script Method info"))
{
Component[] components = gameObject.GetComponents<Component>();
int len = components.Length;
for (int i = 0; i < len; i++)
{
Component com = components[i];
Debug.Log(com.ToString()); //打印出所有组件的名字

if(com is TestReflection)
{
PrintMethods(com);//打印组件里面的所有方法
}

}
}
}

/// <summary>
/// 打印组件里面的所有方法
/// </summary>
/// <param name="com">COM.</param>
void PrintMethods(Component com)
{
System.Type comType = com.GetType();
MethodInfo[] methodInfos = comType.GetMethods();
for (int j = 0; j < methodInfos.Length; j++)
{
MethodInfo minfo = methodInfos[j];
ParameterInfo[] parameters = minfo.GetParameters(); //参数信息
string strParam = "";
for (int k = 0; k < parameters.Length; k++)
{
ParameterInfo param = parameters[k];
strParam += param.ParameterType + " " + param.Name;
if(k < parameters.Length - 1)
strParam += ", ";
}

Debug.Log(string.Format("@{0} {1} ({2})", minfo.ReturnType.ToString(), minfo.Name, strParam));

}
}
}

C#Attribute标签

标签:http://mp.blog.csdn.net/postedit/78123322
代码演示Attribute的用法:
首先定义一个自定义标签类:
MyAttribute.csusing UnityEngine;
using System;

[AttributeUsage(AttributeTargets.Class)] //规定标签使用在什么地方,这里让标签只能用到类上面

//定义一个标签类
public class MyAttribute : Attribute
{
public string DisplayName;

public MyAttribute(string displayName)
{
DisplayName = displayName;
}
}
接着在一个类中使用这个自定义标签,使用方法如下:using UnityEngine;
using System.Collections;

[MyAttribute("反射测试类")]
//[My("反射测试类")] 这种写法也可以

public class TestAttribute : MonoBehaviour
{

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}
然后我们在测试类中查找并打印出刚才定义的标签信息:using UnityEngine;
using System.Collections;
using System.Reflection;

public class TestReflection : MonoBehaviour {

void OnGUI()
{
if(GUI.Button(new Rect(50,50,200,50), "Display Script Method info"))
{
Component[] components = gameObject.GetComponents<Component>();
int len = components.Length;
for (int i = 0; i < len; i++)
{
Component com = components[i];
if( com is TestAttribute)
{
PrintAttributes(com);
}
}
}
}

/// <summary>
/// 打印组件类里面所有的attributes.
/// </summary>
void PrintAttributes(Component com)
{
System.Type attType = com.GetType();
object[] atts = attType.GetCustomAttributes(true); //获取所有的自定义标签
for (int m = 0; m < atts.Length; m++)
{
object attribute = atts[m];
if(attribute is MyAttribute)
{
Debug.Log((attribute as MyAttribute).DisplayName); //显示标签展示名字
}
}
}
}
输出为:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity编辑器 C#脚本