Unity常用的技巧

转自:http://www.manew.com/thread-146358-1-1.html

1、TextArea 
当我们使用“string”要输入比较长的文字的时候,往往会看不全文字
可以使用 “string” 属性的字符串来编辑在文本区域的柔性。
Unity常用的技巧 

2、Header, Tooltip and Space  
为了更好的组织属性字段,可以使用 “Header” 、 “Tooltip” 和 “Space”
来作为Inspector面板上提示作用
Unity常用的技巧 

3、扩展方法 
你可以使用扩展方法向现有功能添加到类别、类型、结构和更多 !
Unity常用的技巧 
Unity常用的技巧
4、#define 定制平台 
可以设定自己的 “#defines定制平台 , 快速添加或删除功能 
Unity常用的技巧 

5、Inspector面板数值编辑速度
当编辑某一个数字时的速度,按“Shift”键,变化更快,按“Alt”键,变化更慢
Unity常用的技巧 

6、unity内置Shader 
可以下载所有的unity 内置的shaders (所有 cginc 文件) 可以通过访问 档案下载页面。
Unity常用的技巧 

7、MinMax属性 

可以使用 MinMax属性,在Inspector面板中 随机 vector2 
Unity常用的技巧 

 

minmaxattribute.cs   minmaxattribute.cs 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

 
using UnityEngine;
// 可以放任何地方 ,但不要放在Editor 文件夹
public class MinMaxAttribute : PropertyAttribute
{
    public float minLimit = 0;
    public float maxLimit = 1;
 
    public MinMaxAttribute(int min, int max)
    {
        minLimit = min;
        maxLimit = max;
    }
}

MinMaxDrawer.cs   MinMaxDrawer.cs 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

 
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering.PostProcessing;
 
// ! -> Put this in an Editor folder
// By @febucci : https://www.febucci.com/
[CustomPropertyDrawer(typeof(MinMaxAttribute))]
public class MinMaxDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //Asserts that we're using the MinMax attribute on a Vector2
        UnityEngine.Assertions.Assert.IsTrue(property.propertyType == SerializedPropertyType.Vector2, "Devi usare un vector2 per MinMax");
 
        if (property.propertyType == SerializedPropertyType.Vector2)
        {
 
            MinMaxAttribute minMax = attribute as MinMaxAttribute;
 
            //Writes the variable name on the left
            Rect totalValueRect = EditorGUI.PrefixLabel(position, label);
 
            //The left value, after the variable name
            Rect leftRect = new Rect(totalValueRect.x, totalValueRect.y, 50, totalValueRect.height);
 
            //Rect of the slider
            Rect valueRect = new Rect(leftRect.xMax, totalValueRect.y, totalValueRect.width - leftRect.width * 2 - 4, totalValueRect.height);
 
            //The right value
            Rect rightRect = new Rect(totalValueRect.xMax - leftRect.width - 2, totalValueRect.y, leftRect.width, totalValueRect.height);
 
            float minValue = property.vector2Value.x; //Current x
            float maxValue = property.vector2Value.y; //Current y
 
            EditorGUI.MinMaxSlider(valueRect, ref minValue, ref maxValue, minMax.minLimit, minMax.maxLimit);
 
            //Assigns the value to the property
            property.vector2Value = new Vector2(minValue, maxValue);
 
            EditorGUI.LabelField(leftRect, minValue.ToString("F3")); //Writes the value on the left
            EditorGUI.LabelField(rightRect, maxValue.ToString("F3")); //Writes the value on the right
        }
        else
        {
            GUI.Label(position, "You can use MinMax only on a Vector2!");
        }
    }
}

8、Curves
可以使用动画曲线通过代码为对象创建炫酷效果。
查看更多关于 “Lerp/Easing”
Unity常用的技巧 

9、单独预览窗口 
可以将预览窗口和检验员的任何地方。( 右键单击它的顶部条)就可以单独窗口显示 
Unity常用的技巧

10、层次组织 
你可以通过添加作为层级空游戏对象。
建议来制定自己的tag标签为 “EditorOnly”在build过程中,这样它们就不会保存。
Unity常用的技巧 

11、颗粒系统回放时间
可以控制颗粒的回放时间中的编辑器来编辑效果更好。
Unity常用的技巧

12、EditorOnly标签 
可以使用“EditorOnly”Tag标记从最终构建中排除GameObject。
Unity常用的技巧 

13、分配的材料在创建着色器
当我们创建材质时,总忘了是哪一个自定义shader,然而
可以选中shader》鼠标右击》选择material,即可创建对应的shader材质
Unity常用的技巧 

14、插入数据在列表数组
当我们需要填一些数据时,有些是一样的数据,我们都是手动添加,其实
可以通过按快捷键“CTRL + D”(代表:“复制”)在Inspector中插入数组元素,而无需手动调整下一个元素。
Unity常用的技巧 

15、显示了多个文件夹的内容 
你能看到的内容的多个文件夹中选择它, 只需在 Project 窗口中。
Unity常用的技巧 

16、定制Unity 的C#脚本模板
可以自定义 Unity 中的模板通过改变 “81-C#” 中找到文件路径 : "%EDITOR_PATH%\Data\Resources\ScriptTemplates" 。
记得在修改前存储拷贝 !
P. s.# C - 81保存一份原始文件 , 我建议移动到另一文件夹
Unity常用的技巧

17、控制台日志条目窗口
你可以自行调整线的每个日志显示在控制台窗口中 , 以读取更多 (或更少) 文本。
Unity常用的技巧 

18、OnValidate
脚本被修改了之后就会执行
在这里阅读更多。
Unity常用的技巧 

19、设置为预设默认编辑器
您可以设置预设编辑器的默认状态 , 然后按下 “复位”
Unity常用的技巧 

20、在播放模式下编辑颜色 

你可以提醒自己在编辑器处于播放模式时 , 通过改变其 “着色方式” 。

 

https://febucci.com/wp-content/uploads/febucci-unitytips-playmode-editor-color.gif


21、不一样的控制台输出 
打印不一样的样
   Unity常用的技巧 

22、在项目窗口中搜索资源
    Unity常用的技巧 
23、DisallowMultipleComponent & RequireComponent 
您可以使用DisallowMultipleComponent和RequireComponent属性来避免游戏对象中的设置错误。
   Unity常用的技巧 

24、Graphy 
可以使用@ tayx94制作的这款精彩工具
直接从发布出来中监控游戏的性能。
GitHub repository 地址:https://github.com/Tayx94/graphy
   Unity常用的技巧 

25、NaughtyAttributes
可以在GitHub上使用dbrizov制作的20多个属性扩展,比如ReorderableList,Slider或ResizableTextArea。
GitHub repository 地址:https://github.com/dbrizov/NaughtyAttributes
   Unity常用的技巧 

26、Foldout属性显示管理 
可以使用Dimmpixeye在GitHub上创建的Foldout属性在Inspector中对变量进行分组。
GitHub Foldout地址:https://github.com/dimmpixeye/InspectorFoldoutGroup
   Unity常用的技巧 

27、ContextMenu上下文菜单
使用ContextMenu属性从Inspector调用方法
   Unity常用的技巧 
28、Inspector 面板数学计算
你可以在面板上面进行计算
   Unity常用的技巧 
29、SerializeField & HideInInspector序列化
使用SerializeField和HideInInspector属性选择要显示和序列化的变量。
   Unity常用的技巧 
30、FormerlySerializedAs
如果需要重命名变量,但又不能丢失其序列化值,则非常有用
   Unity常用的技巧 
31、AddComponentMenu 
在Inspector的“AddComponent”菜单中组织脚本,使用AddComponentMenu属性。
   Unity常用的技巧 
32、MenuItem 
方法调用的主菜单、上下文菜单。要注意 , 它只能在编辑的 , 所以你需要它 , 如果你想释放代码。(添加# if _ 统一编辑你就可以开始工作) 。
   Unity常用的技巧