在Unity的Inspector面板显示一个按钮
这个脚本应该放置Unity的Assets文件夹下的Editor文件夹下。
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace wyhTools
{
[CustomEditor(typeof(InspectorShortcutButton))] // 替换"YourScript"为你的脚本类名
public class SetRedPointPos : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
InspectorShortcutButton script = (InspectorShortcutButton)target;
// 添加一个按钮
if (GUILayout.Button("通过DeviceBox设置SelectedBox"))
{
// 获取到这个摄像机
//参数的传递好像是只能从继承自Editor的脚本到MonoBehaviour的脚本,
GameObject gameObjectParameter = GameObject.Find("Main Camera").gameobject;
script.YourFunc(gameObjectParameter );
}
}
}
}
这个脚本可以放置于Scripts文件夹下,然后再物体上挂载这个脚本就会显示一个按钮,并再非运行情况下给摄像机创建一个子物体。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace wyhTools
{
public class InspectorShortcutButton : MonoBehaviour
{
/// <summary>
/// 你的脚本
/// </summary>
/// <param name="model">参数1 </param>
public void YourFunc(GameObject model)
{
if (model != null)
{
//给传递过来的物体创建一个空的子物体
GameObject gameObject_PortList = new GameObject("PortList");
gameObject_PortList.transform.position = new Vector3(0, 0, 0);
gameObject_PortList.transform.parent = model.transform.parent;
}
}
}
}
标签:脚本,GameObject,PortList,Unity,按钮,using,Inspector
From: https://www.cnblogs.com/goodcooking/p/17772739.html