unity 怎么在unity inspector 扩展面板+按钮

Create a Button in the inspector - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Create a Button in the inspector
I'm learning editor scripting, and I can create custom windows, and wizards. But how do you create a button in the inspector? So you click on an object, and under the script title, is a button.
Best Answer
Its very easy:
@CustomEditor(YourScript)
// ^ This is the script we are making a custom editor for.
public class YourScriptEditor extends Editor {
override function OnInspectorGUI () {
//Called whenever the inspector is drawn for this object.
DrawDefaultInspector();
//This draws the default screen.
You don't need this if you want
//to start from scratch, but I use this when I'm just adding a button or
//some small addition and don't feel like recreating the whole inspector.
if(GUILayout.Button(&Your ButtonText&)) [
//add everthing the button would do.
The key part is to create a custom inspector, and override OnInspectorGUI. From there you have much more freedom in what you want to do. DrawDefaultInspector() will draw the inspector exactly like Unity would so it is useful if you are adding functionality to the end or a small addition because you can draw the default inspector then attach more functionality to the end like the above script does.
Of course, if you wanted, you could create a custom inspector from scratch, recreating every inspector field and such, but if you just need a button, that would probably be more work than you want to do.
Here's a C# version:
[CustomEditor(typeof(DecalMeshHelper))]
class DecalMeshHelperEditor : Editor {
public override void OnInspectorGUI() {
if(GUILayout.Button(&Test&))
Debug.Log(&It's alive: & + target.name);
It's very important to use `public override`, otherwise it won't work!
I know this is extremely old, but for anyone looking here years later, there's an easier makeshift way to add buttons!
[ExecuteInEditMode]
public class myClass : Monobehaviour
public bool buttonDisplayN //&run& or &generate& for example
public bool buttonDisplayName2; //supports multiple buttons
if (buttonDisplayName)
ButtonFunction1 ();
else if (buttonDisplayName2)
ButtonFunction2 ();
buttonDisplayName =
buttonDisplayName2 =
void ButtonFunction1 ()
void ButtonFunction2 ()
In the editor, update is called whenever things are changed. When you press one of our &buttons&, the Boolean value changes to true, and triggers Update() immediately. This runs the button function a single time, and at the end of update, turns the Boolean back to false. This allows you to not have to make a separate editor script, and gives the exact functionality, but just a tinier button.
Also, in C#, be sure to use the UnityEditor namespace so that we can actually use the &Editor& class. Otherwise, you'll probably get errors.
So at the top, be sure to put the &using UnityE&
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
7 People are following this question.NGUI怎么把变量显示在Inspector面板上
- Unity3d技术 - 泰课在线 - 国内专业的Unity在线学习平台|Unity3d培训|Unity教程|Unity教程 Unreal 虚幻 AR|移动开发|美术CG|UI平面设计|前端开发 - Powered By EduSoho}

我要回帖

更多关于 unity inspector 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信