在Unity开发游戏的时候,为了有一个更快更方便的工作流,我们往往会在Editor下开发一些方便实用的工具。在工具中,用到最多,最关键的就是按钮,它是工具的首席执行官。下面就用最简单的代码来演示添加一个自定义按钮到Inspector当中。 案例:指定坐标后克隆新物体到场景。 1、 在Unity Assets下创建”ObjectBuilderScript”脚本,添加代码: - <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">using UnityEngine;<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> </span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">public class ObjectBuilderScript
- : MonoBehaviour {<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> public GameObject obj;<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> public Vector3 spawnPoint;<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> </span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> public void BuildObject() {<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> Instantiate(obj, spawnPoint,
- Quaternion.identity);<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> }<o:p></o:p></span></strong></p>
- <p class="MsoNormal" style="margin-left:39.0pt"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">}</span></strong></p>
复制代码
2、 在Unity Assets下创建”Editor”文件夹,文件夹下创建”ObjectBuilderEditor”脚本,添加代码: - <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">using UnityEngine;<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">using UnityEditor;<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> </span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">[CustomEditor(typeof(ObjectBuilderScript))]<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">public class
- ObjectBuilderEditor : Editor {<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> public override void OnInspectorGUI() {<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> DrawDefaultInspector();<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> </span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> ObjectBuilderScript myScript =
- (ObjectBuilderScript)target;<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> if(GUILayout.Button("</span></strong><strong><span style="font-family:宋体;mso-ascii-font-family:
- Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
- minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;
- mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi;="" font-weight:normal"="">创建对象</span></strong><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">")) {<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> myScript.BuildObject();<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> }<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"=""> }<o:p></o:p></span></strong></p>
- <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
- 0"><strong><span lang="EN-US" calibri",sans-serif;mso-ascii-theme-font:minor-latin;="" mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"times="" new="" roman";="" mso-bidi-theme-font:minor-bidi;font-weight:normal"="">}</span></strong></p>
复制代码
3、 场景中添加一个空的GameObject对象,挂载上” ObjectBuilderScript”脚本,在Inspector中选中需要克隆的游戏对象,填入克隆对象的出生点坐标,点击“创建对象”按钮,就将克隆对象添加到场景当中。如图: 活跃的Unity技术交流qq群:935714213
|