GUILayout.Button
记录使用GUILayout.Button在编辑器绘制Button按钮
创建脚本UIEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(TestUI))]
public class UIEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
TestUI test = (TestUI)target;
if (GUILayout.Button("创建对象"))
{
test.CreateObject();
Debug.Log("创建对象成功");
}
}
}
创建脚本TestUI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestUI : MonoBehaviour
{
GameObject image;
Vector3 Vec=new Vector3(50,490,0);
int index = 0;
Transform parent;
private void Start()
{
image = Resources.Load<GameObject>("Object/image");
parent= GameObject.Find("Canvas").transform;
}
public void CreateObject()
{
GameObject img=Instantiate(image,Vec+new Vector3(index*150,0,0),Quaternion.identity);
img.transform.parent = parent;
index++;
}
}
将脚本挂载在场景中,如图所示: 将物体image制作成预制体,放置于Resource文件夹下的Object文件夹中 运行,点击按钮,就能直接生成物体
|