我必须在运行时通过拖动按钮来创建对象。我找了很多,但还是找不到解决办法。
我通过使用以下示例代码单击按钮来创建对象。但我不得不拖着脚。
我的代码是:
using UnityEngine;
using System.Collections;
public class CreateSphere : MonoBehaviour {
public void OnClick()
{
GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
sphere.transform.localScale = new Vector3(3.0f, 3.0f, 1.0f) ;
//transform.Translate(0, 0, Time.deltaTime);
sphere.transform.position = new Vector3 (4, 0, 0);
}
}
OnClick
不是拖动,只是单击。要拖动,您需要有拖动处理程序
using UnityEngine;
using UnityEngine.GUI;
using UnityEngine.EventSystems;
using System.Collections;
public class CreateSphere : MonoBehaviour, IBeginDragHandler, IDragHandler {
GameObject sphere;
public void OnBeginDrag(PointerEventData data)
{
// Begin dragging logic goes here
sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
}
public void OnDrag(PointerEventData data)
{
// Dragging logic goes here
sphere.transform.localScale = new Vector3(3.0f, 3.0f, 1.0f) ;
//transform.Translate(0, 0, Time.deltaTime);
sphere.transform.position = new Vector3 (4, 0, 0);
}
}