unity colorusaunity rangeattributee怎么了

【求助】关于settexture,setcolor不能实时更新的疑问_unity3d吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:64,390贴子:
【求助】关于settexture,setcolor不能实时更新的疑问收藏
脚本内void Start(){
brakelightsMesh[i].material.SetTexture (&_EmissionMap&, lightstexture[0]);
brakelightsMesh[i].material.SetColor(&_EmissionColor&,Color.red);}数组全部定义过了,添加(gettexture和getcolor)也都能正常获取但运行结果如下图必须要在编辑器内点一下材质栏,才能有效果。如下图什么都没改动,只是点了一个打开材质球的操作。就有效果了。怎么才能实时更新呢。
京东电脑节,全民抢宝进行时!1999抢i7本,半价秒电脑,抢直降3000显示器
写到Update中试试
LZ问题解决了吗?我遇到了一样的问题
之前也遇到这个问题,然后换了一种方法,坐等大神,感觉是要刷一下材质
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Unity3d 基础知识(13)
Attribute是C#的功能,在Unity中可以使用Attribute来给变量和方法增加新的特性或者功能。
举两个例子,在变量上使用[SerializeFiled]特性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值。
在Class上使用[RequireComponent]特性,就会在Class的GameObject上自动追加所需的Component。
以下是Unity官网文档中找到的所有Attribute,下面将按照顺序,逐个对这些Attribute进行说明和小的测试。
部分例子使用了Unity官方的示例。
UnityEngine
AddComponentMenu
可以在UnityEditor的Component的Menu中增加自定义的项目。菜单可以设置多级,使用斜线/分隔即可。在Hierarchy中选中GameObject的时候,点击该菜单项,就可以在GameObject上追加该Component。
例如如下代码可以完成下图的效果。
[AddComponentMenu("TestMenu/TestComponet")]
public class TestMenu : MonoBehaviour {
ContextMenu
可以在Inspector的ContextMenu中增加选项。
例如,如下代码的效果
public class TestMenu : MonoBehaviour {
[ContextMenu ("Do Something")]
void DoSomething () {
Debug.Log ("Perform operation");
ContextMenuItemAttribute
这个属性是Unity4.5之后提供的新功能,可以在Inspector上面对变量追加一个右键菜单,并执行指定的函数。
public class Sample : MonoBehaviour {
[ContextMenuItem("Reset", "ResetName")]
public string name = "Default";
void ResetName() {
name = "Default";
DisallowMultipleComponent
对一个MonoBehaviour的子类使用这个属性,那么在同一个GameObject上面,最多只能添加一个该Class的实例。
尝试添加多个的时候,会出现下面的提示。
ExecuteInEditMode
默认状态下,MonoBehavior中的Start,Update,OnGUI等方法,需要在Play的状态下才会被执行。
这个属性让Class在Editor模式(非Play模式)下也能执行。
但是与Play模式也有一些区别。
Update方法只在Scene编辑器中有物体产生变化时,才会被调用。
OnGUI方法只在GameView接收到事件时,才会被调用。
HeaderAttribute
这个属性可以在Inspector中变量的上面增加Header。
public class ExampleClass : MonoBehaviour {
[Header("生命值")]
public int CurrentHP = 0;
public int MaxHP = 100;
[Header("魔法值")]
public int CurrentMP = 0;
public int MaxMP = 0;
HideInInspector
在变量上使用这个属性,可以让public的变量在Inspector上隐藏,也就是无法在Editor中进行编辑。
ImageEffectOpaque
在OnRenderImage上使用,可以让渲染顺序在非透明物体之后,透明物体之前。
[ImageEffectOpaque]
MultilineAttribute
在string类型上使用,可以在Editor上输入多行文字。
public class TestString : MonoBehaviour {
[MultilineAttribute]
public string mT
PropertyAttribute
RangeAttribute
在int或者float类型上使用,限制输入值的范围
public class TestRange : MonoBehaviour
[Range(0, 100)] public int HP;
RequireComponent
在Class上使用,添加对另一个Component的依赖。
当该Class被添加到一个GameObject上的时候,如果这个GameObject不含有依赖的Component,会自动添加该Component。
且该Componet不可被移除。
[RequireComponent(typeof(Rigidbody))]
public class TestRequireComponet : MonoBehaviour {
如果尝试移除被依赖的Component,会有如下提示
在方法上添加该属性,可以网络通信中对该方法进行RPC调用。
void RemoteMethod(){
RuntimeInitializeOnLoadMethodAttribute
此属性仅在Unity5上可用。
在游戏启动时,会自动调用添加了该属性的方法。
class MyClass
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad ()
Debug.Log("Game loaded and is running");
SelectionBaseAttribute
当一个GameObject含有使用了该属性的Component的时候,在SceneView中选择该GameObject,Hierarchy上面会自动选中该GameObject的Parent。
SerializeField
在变量上使用该属性,可以强制该变量进行序列化。即可以在Editor上对变量的值进行编辑,即使变量是private的也可以。
在UI开发中经常可见到对private的组件进行强制序列化的用法。
public class TestSerializeField : MonoBehaviour {
[SerializeField]
private string
[SerializeField]
private Button _
SharedBetweenAnimatorsAttribute
用于StateMachineBehaviour上,不同的Animator将共享这一个StateMachineBehaviour的实例,可以减少内存占用。
SpaceAttribute
使用该属性可以在Inspector上增加一些空位。 例子:
public class TestSpaceAttributeByLvmingbei : MonoBehaviour {
public int nospace1 = 0;
public int nospace2 = 0;
[Space(10)]
public int space = 0;
public int nospace3 = 0;
TextAreaAttribute
该属性可以把string在Inspector上的编辑区变成一个TextArea。
public class TestTextAreaAttributeByLvmingbei : MonoBehaviour {
[TextArea]
public string mT
TooltipAttribute
这个属性可以为变量上生成一条tip,当鼠标指针移动到Inspector上时候显示。
public class TestTooltipAttributeByLvmingbei : MonoBehaviour {
[Tooltip("This year is 2015!")]
public int year = 0;
UnityEngine.Serialization
FormerlySerializedAsAttribute
该属性可以令变量以另外的名称进行序列化,并且在变量自身修改名称的时候,不会丢失之前的序列化的值。
using UnityE
using UnityEngine.S
public class MyClass : MonoBehaviour {
[FormerlySerializedAs("myValue")]
private string m_MyV
public string myValue
get { return m_MyV }
set { m_MyValue = value; }
CustomPreviewAttribute
将一个class标记为指定类型的自定义预览
Unity4.5以后提供的新功能
[CustomPreview(typeof(GameObject))]
public class MyPreview : ObjectPreview
public override bool HasPreviewGUI()
return true;
public override void OnPreviewGUI(Rect r, GUIStyle background)
GUI.Label(r, target.name + " is being previewed");
可以在Scene视图中显示自定义的Gizmo
下面的例子,是在Scene视图中,当挂有MyScript的GameObject被选中,且距离相机距离超过10的时候,便显示自定义的Gizmo。
Gizmo的图片需要放入Assets/Gizmo目录中。
可以在Scene视图中显示自定义的Gizmo
下面的例子,是在Scene视图中,当挂有MyScript的GameObject被选中,且距离相机距离超过10的时候,便显示自定义的Gizmo。
Gizmo的图片需要放入Assets/Gizmo目录中。
InitializeOnLoadAttribute
在Class上使用,可以在Unity启动的时候,运行Editor脚本。
需要该Class拥有静态的构造函数。
做一个创建一个空的gameobject的例子。
using UnityE
using UnityE
[InitializeOnLoad]
class MyClass
static MyClass ()
EditorApplication.update += U
Debug.Log("Up and running");
static void Update ()
Debug.Log("Updating");
在方法上使用,可以在Editor中创建一个菜单项,点击后执行该方法,可以利用该属性做很多扩展功能。 需要方法为static。
using UnityE
using UnityE
using System.C
public class TestMenuItem : MonoBehaviour {
[MenuItem ("MyMenu/Create GameObject")]
public static void CreateGameObject() {
new GameObject("lvmingbei's GameObject");
PreferenceItem
使用该属性可以定制Unity的Preference界面。
在这里就使用官方的例子:
using UnityE
using UnityE
using System.C
public class OurPreferences {
private static bool prefsLoaded = false;
public static bool boolPreference = false;
[PreferenceItem ("My Preferences")]
public static void PreferencesGUI () {
if (!prefsLoaded) {
boolPreference = EditorPrefs.GetBool ("BoolPreferenceKey", false);
prefsLoaded = true;
boolPreference = EditorGUILayout.Toggle ("Bool Preference", boolPreference);
if (GUI.changed)
EditorPrefs.SetBool ("BoolPreferenceKey", boolPreference);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:26908次
排名:千里之外
原创:54篇
转载:42篇
(1)(1)(1)(7)(1)(1)(5)(9)(20)(8)(22)(9)(6)(8)5.1 Editing Properties_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
5.1 Editing Properties
上传于|0|0|暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩4页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢这篇文章主要讲一下C#里面Attribute的使用方法及其可能的应用场景。
比如你把玩家的血量、攻击、防御等属性写到枚举里面。然后界面可能有很多地方要根据这个枚举获取属性的描述文本。
比如你做网络框架的时候,一个协议号对应一个类的处理或者一个方法。
比如你做ORM,一个类的属性是否映射持久化文件中的属性,映射过去的属性名是什么。
什么是Attribute
如果用过Java的Annotation的同学,可以把Attribute当成Annotation来看待。
还不了解Attribute的同学不用急,我们看一下官方的解释:
The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event field,interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.
Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.
All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied t multiple attributes can be applied to the and attributes can be inherited by an element derived from a target element. Use the
class to specify the target element to which the attribute is applied.
The Attribute class provides convenient methods to retrieve and test custom attributes. For more information about using attributes, see
翻译过来就是:
Attribute类可以把目标元素和一个预定义的信息或者是用户自定义信息关联起来。这里的目标元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可执行文件模块,parameter,property,return value,struct或其它的Attribute。
Attribute提供的信息也被称为元数据(metadata)。元数据能用于在运行时控制怎样访问你的程序数据,或者在运行前通过额外的工具来控制怎样处理你的程序或部署它。例如.NET Framework预定义并使用attribute去控制运行时行为,一些编程语言使用attribute类型来描述.NET Framework中通用类型不直接支持的语言特性。
所有的Attribute类型直接或间接从Attribute类继承。Attribute能应用到任何target元素;多个Attribute能应用到相同的元素;
Attribute类提供遍历的方法去取出和测试自定义Attribute。更多关于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。
如果你看了官方的解释不明白,看了我的翻译也不明白。也没事。。。我们接下来举个例子看看Attribute能做啥。
用Attribute将枚举和一个描述文本绑定在一起
假设有这个枚举
public enum Properties
PhyAtk = 2,
PhyDef = 3,
MagAtk = 4,
MagDef = 5
然后你现在想要根据枚举来获得中文描述:比如传入:
Properties.MagDef返回“法术防御”。
最原始的做法:
public class PropertiesUtils
public static string GetDescByProperties(Properties p)
switch (p)
case Properties.HP:
return "血量";
case Properties.PhyAtk:
return "物理攻击";
case Properties.PhyDef:
return "物理防御";
case Properties.MagAtk:
return "法术攻击";
case Properties.MagDef:
return "法术防御";
return "未知属性:" +
这样确实可以解决问题,但是我们可以用Attribute来做的更好。可以做的更好干嘛不呢?
先定义一个用于存储描述文本的Attribute。
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Enum)]
public class PropertiesDesc : System.Attribute
public string Desc { get; private set; }
没错,看起来是不是觉得很简单。
然后我们就可以把上面定义的PropertiesDesc加到Properties上面,像这样:
public enum Properties
[PropertiesDesc("血量")]
[PropertiesDesc("物理攻击")]
PhyAtk = 2,
[PropertiesDesc("物理防御")]
PhyDef = 3,
[PropertiesDesc("法术攻击")]
MagAtk = 4,
[PropertiesDesc("法术防御")]
MagDef = 5
OK。这样,我们相当于就把一个文本描述信息通过Attribute关联到我们的枚举属性了。
那么怎样获取?我们来重写之前的PropertiesUtils类。
public class PropertiesUtils
public static string GetDescByProperties(Properties p)
Type type = p.GetType();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo field in fields)
if (field.Name.Equals(p.ToString()))
object[] objs = field.GetCustomAttributes(typeof(PropertiesDesc), true);
if (objs != null && objs.Length & 0)
return ((PropertiesDesc)objs[0]).D
return p.ToString() + "没有附加PropertiesDesc信息";
return "No Such field : "+p;
可以看到。这里面已经不用自己去判断哪个枚举值返回哪个字符串描述了。而是获取这个枚举域的PropertiesDesc对象。然后返回它的Desc属性。
当然,你还可以把上面的代码改成通用的,把Properties改成一个Type,这样就可以处理所有的枚举。然后还可以在查找PropertiesDesc的位置增加一个缓存。根据Type和字段的Name做缓存。改完后代码如下:
public class PropertiesUtils
static Dictionary&Type, Dictionary&string, string&& cache = new Dictionary&Type, Dictionary&string, string&&();
public static string GetDescByProperties(object p)
var type = p.GetType();
if (!cache.ContainsKey(type))
Cache(type);
var fieldNameToDesc = cache[type];
var fieldName = p.ToString();
return fieldNameToDesc.ContainsKey(fieldName) ? fieldNameToDesc[fieldName] : string.Format("Can not found such desc for field `{0}` in type `{1}`", fieldName, type.Name);
private static void Cache(Type type)
var dict = new Dictionary&string, string&();
cache.Add(type, dict);
var fields = type.GetFields();
foreach (var field in fields)
var objs = field.GetCustomAttributes(typeof(PropertiesDesc), true);
if (objs.Length & 0)
dict.Add(field.Name, ((PropertiesDesc)objs[0]).Desc);
还能干什么?
Attribute能干的事情太多了,比如你写了个类,想做ORM映射,里面有些字段不想映射到表,有些想映射到表。有些字段可能名字和表的字段不一样。这时候你就可以通过Attribute来标识哪个字段需要被映射,映射到数据库哪个字段。等等。
做过网络框架的同学也应该比较熟悉的一个应用,使用Attribute来做自动的消息派发。
总之,Attribute可以做很多自动化的事情,就看你怎么用了。
以上内容来自转载,下面做一些自己的补充
在我们获取FiledInfo的时候,可以根据来进行选择,例如:type.GetFields(BindingFlags.NonPublic)表示指定非公共成员将包括在搜索中。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:25594次
排名:千里之外
原创:52篇
转载:14篇
(3)(4)(6)(10)(6)(19)(1)(4)(1)(1)(1)(2)(5)(4)(7)Unity的Attribute(特点)还算多吧 - 移动开发当前位置:& &&&Unity的Attribute(特点)还算多吧Unity的Attribute(特点)还算多吧&&网友分享于:&&浏览:0次Unity的Attribute(特性)还算多吧转载请注明出处:http://blog.csdn.net/u更全的内容请看我的游戏蛮牛地址:/space-uid-18602.html& 属性 (Attribute)使用 Unity 的C#语言 ,利用属性(Attribute)来类定义和变量定义或区分其他的变量,您可以设置一种特殊行为。* 1例如,您添加[SerializeField]属性变量,私有变量标识序列化。&[SerializeField]&int& 计数; 序列化您'的值是存储到场景和预制体。因为值存储在meta元数据中,在Inspector检查器中设置的值。&你还可以强制指定的组件,对象要被附加组件,您将添加[RequireComponent] * 2 * 3 。[RequireComponent(typeof(Rigidbody))]public classAttributeSample : MonoBehaviour {}你可以使用属性时,设置特殊的变量命名约定以外的其他行为。unity使用属性,设置特约编辑和方法的行为。&规则属性属性放置在[below. * * *]的窗体变量和类定义之前。如果你想要设置多个属性,以逗号分隔 [* * * . &* * *]的写入操作。例如,如果您想要设置的 SerializeField 和范围,如描述属性。& [SerializeField, Range(0, 5)]&&& 属性适用于立即定义的所有变量。例如,如果在一个声明中定义多个变量[SerializeField, Range(0,5)]&&& int count3, count4;以及适合数组,是适用于所有的变量,使其包含。[SerializeField, Range(0,5)]&&& int[]统一标准属性要扩展Inspector检查器属性扩展的Inspector检查器行为选择字段。oSerializeFieldprivate或protected值的序列化。如果您想要在场景视图中进行编辑,它非常有用。[SerializeField]&oTooltipAttribute如果鼠标光标是在字段上,显示的说明文本。&&& [SerializeField,TooltipAttribute(&説明文&)]int count5;&oSpaceAttribute设置字段和字段之间的空间。[SerializeField,Space(15)]intcount6;&&&&&&&&&& &oHeaderAttribute设置标题,就是字段的头部。需要注意,因为标题给予所有项目和将 HeaderAttribute 添加到列表中[SerializeField,HeaderAttribute (&Title&)]int count7;&oMultilineAttribute设置多行输入的文本字段。[SerializeField, MultilineAttribute(2)]string message1;&oTextAreaAttribute设置多行输入的文本字段。您可以设置的最大值和最小值的行数。[SerializeField,TextAreaAttribute(2, 5)]&string message2;&oHideInInspector这种public序列化字段从Inspector检查器隐藏。如果隐藏的参数、扩展的编辑器中内置的参数和引用关系。请注意不能从检查器编辑,HideInInspector 用于构建unity场景隐藏一个序列化的值,当声明一个字段变量初始化也将忽略。使用NonSerializable的序列化和HideInInspector。[HideInInspector]&&& public int count8;oNonSerializable序列化并不妨碍您将从检查器中消失。&& & [System.NonSerialized]public int count9;&oFormerlySerializedAs当您更改该变量的名称,保持信息的修改。unity保持信息字段字段名称中,字段名称被更改和销毁值。当您可以指定FormerlySerializedAs此信息将被带到目标变量的名称。 首先,所有的变量first。我试着改变这第二个变量中。并添加 FormerlySerializedAs 属性,原& first &。[FormerlySerializedAs(&first&)]& &&&&&&&& &即使您更改中的变量名称的值first在此变量的名称second之后,将保留。如果你在其他场景中使用可迁移要删除 FormerlySerializedAs 和各种的麻烦,请注意。&与组件的行为相关联的属性oRequireComponentAttribute添加组件的行为附加到对象。例如,刚体等......[RequireComponent(typeof(Rigidbody))]public classAttributeSample : MonoBehaviour {}&oDisallowMultipleComponent不能将几个组件添加到同一个对象(就是不能重复)。[DisallowMultipleComponent]public classAttributeSample : MonoBehaviour {}&oContextMenuAttribute我想要从组件的上下文菜单(单击右键)调用方法。例如,如果您在运行时做不被动设置很有用。[ContextMenu(&Init&)]void Init(){}&影响游戏的行为的属性oRPC在执行 RPC 时,它使用。用于在 NetworkView 中的光子与沟通。此属性是跨网络调用方法。[RPC]void Damage(){}&oImageEffectTransformsToLDR似乎转换HDR和 LDR 。如何使用未知。oImageEffectOpaque使 OnRenderImage 透明呈现。[ImageEffectOpaque]void OnRenderImage(RenderTexture source, RenderTexture destination){}&oMonoPInvokeCallbackAttributeC#(托管代码) 中注册方法可以从c + +(非托管代码) 调用。&oDLLImportC + +(非托管代码) 的方法可从c#调用。[DllImport(&DLLName&)]&private static extern void MethodName();&用编辑器的行为相关联的属性是可以影响的场景从编辑器的顶部的属性。oAddComponentMenu设置路径,当指定 AddComponent 按钮和菜单栏构件。如果你不做你的Script /Namespace 和组件的名称。[AddComponentMenu(&Sample/TestCode&)]public class AttributeSample: MonoBehaviour{}&oExecuteInEditMode状态不玩游戏等组件事件更新并开始工作。用来验证是否要工作在运行时的行为。[ExecuteInEditMode]public classAttributeSample : MonoBehaviour {}ExecuteInEditMode操作热点像代码更改为序列化的形式可以是值是序列化→→汇编和注射,不能破坏价值和可能。正因为如此,注意力是需要,因为需要发挥出来的场景将取决于静态代码或初始化开始等......例如是前 3 名,代码更改和运行编译下面的代码的代码更改,并打印 0。static int sCount = 0;void Start(){&sCount = 3;}void Update(){&& Debug.Log(&export:& + sCount);}oSelectionBaseAttribute选择您选择在场景视图中使用此属性的组件。[SelectionBase]public class AttributeSample :MonoBehaviour {}例如授予的授予对父对象的渲染器模型的SelectionBaseAttribute 选定在场景视图的模型时,选择场景视图组件所选的父对象上,当你选择时。是到游戏对象的 SelectionBase对象,如果不是根据行为给予的批。格兰特获取未授予一个球体,但游戏物体的对象。&
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有

我要回帖

更多关于 attributeusage 的文章

 

随机推荐