GameRes游资网

 找回密码
 立即注册
返回列表
查看: 79|回复: 0

Unity Inspector添加自定义按钮(Button)

[复制链接]
发表于 2019-1-2 17:30:23 | 显示全部楼层 |阅读模式
游戏程序
平台类型: PC/兼容机 
程序设计: 客户端 
编程语言: C# 
引擎/SDK: Unity3D 
         Unity开发游戏的时候,为了有一个更快更方便的工作流,我们往往会在Editor下开发一些方便实用的工具。在工具中,用到最多,最关键的就是按钮,它是工具的首席执行官。下面就用最简单的代码来演示添加一个自定义按钮到Inspector当中。
         案例:指定坐标后克隆新物体到场景。
1、  在Unity Assets下创建”ObjectBuilderScript”脚本,添加代码:
  1. <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>

  2. <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>

  3. <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
  4. : MonoBehaviour {<o:p></o:p></span></strong></p>

  5. <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>

  6. <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>

  7. <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>

  8. <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>

  9. <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,
  10. Quaternion.identity);<o:p></o:p></span></strong></p>

  11. <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>

  12. <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”脚本,添加代码:
  1. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  2. 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>

  3. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  4. 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>

  5. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  6. 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>

  7. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  8. 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>

  9. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  10. 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
  11. ObjectBuilderEditor : Editor {<o:p></o:p></span></strong></p>

  12. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  13. 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>

  14. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  15. 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>

  16. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  17. 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>

  18. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  19. 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 =
  20. (ObjectBuilderScript)target;<o:p></o:p></span></strong></p>

  21. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  22. 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:
  23. Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:
  24. minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;
  25. 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>

  26. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  27. 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>

  28. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  29. 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>

  30. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  31. 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>

  32. <p class="MsoListParagraph" style="margin-left:39.0pt;text-indent:0cm;mso-char-indent-count:
  33. 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中选中需要克隆的游戏对象,填入克隆对象的出生点坐标,点击“创建对象”按钮,就将克隆对象添加到场景当中。如图:
1.png
         更多Unity相关技术学习交流推荐Paws3D学习中心:https://www.paws3d.com/learn/
         活跃的Unity技术交流qq群:935714213
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|作品发布|文章投稿|广告合作|关于本站|GameRes游资网 ( 闽ICP备05005107-1 )

GMT+8, 2019-1-18 23:26

快速回复 返回顶部 返回列表