码迷,mamicode.com
首页 > 编程语言 > 详细

unity 5.3 中新增加的 Attribute

时间:2015-11-26 11:01:17      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

孙广东  2015.11.24

1、PreserveAttribute


         PreserveAttribute 可 从删除的类、 方法、 字段或属性的字节代码中 剥离出来 。

         当你创建一个build 时,Unity 会尽量从您的项目中去掉不用的代码。这是最大限度的得到最小生成 。然而,有时你想要一些代码来不被删除,即使它看起来好像没有什么用 。如果你使用反射 来调用方法 或 实例化某个类 的对象。您可以使用[Preserve] 特性应用于 类、 方法、 字段和属性。除了使用 PreserveAttribute,你也可以使用 link.xml 文件的传统方法  来告诉链接器不删除的东西。PreserveAttribute 只在 IL2CPP 平台上运行。Link.xml 文件的工作于 Mono  和 IL2CPP 平台。


using UnityEngine;
using System.Collections;
using System.Reflection;
using UnityEngine.Scripting;

public class NewBehaviourScript : MonoBehaviour {
	void Start () {
	   ReflectionExample.InvokeBoinkByReflection();
	}
}

public class ReflectionExample
{
	 static public void InvokeBoinkByReflection()
	 {
	 	typeof(ReflectionExample).GetMethod("Boink", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null,null);
	 }

	 // No other code directly references the Boink method, so when when stripping is enabled,
   // it will be removed unless the [Preserve] attribute is applied.
	 [Preserve]
	 static void Boink()
	 {
	 	Debug.Log("Boink");
	 }
}

         对于不想倚赖 UnityEngine.dll 的第三方库,它也是可以定义自己的 PreserveAttribute。代码会尊重这一点,并会考虑任何属性具有确切名称 "PreserveAtribute" 应用不删除的东西上,无论该 命名空间或程序集的 属性。


2、为类提供一个自定义的文档的URL。

[HelpURL("http://example.com/docs/MyComponent.html")]
public class MyComponent
{
   
}

3、允许当Unity游戏 从用户加载运行时     运行时 类方法 被初始化 。

     场景已被加载 (即 Awake 方法已被调用),将 调用 标记 [RuntimeInitializeOnLoadMethod] 的方法。注: 标记 [RuntimeInitializeOnLoadMethod] 的方法的执行顺序是不能保证的。

using UnityEngine;

class MyClass
{
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad ()
    {
        Debug.Log("After scene is loaded and game is running");
    }

    [RuntimeInitializeOnLoadMethod]
    static void OnSecondRuntimeMethodLoad ()
    {
        Debug.Log("SecondMethod After scene is loaded and game is running.");
    }
}


4、

using UnityEngine;
        
[SharedBetweenAnimators]
public class AttackBehaviour : StateMachineBehaviour
{
	public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
	{
		Debug.Log("OnStateEnter");
	}
}







??
??

unity 5.3 中新增加的 Attribute

标签:

原文地址:http://blog.csdn.net/u010019717/article/details/50005049

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!