Unity 如何使用鼠标点击缩放游戏对象?

Unity how to scale gameobjects using mouseclick?

有谁知道如何使用鼠标单击来放大不同的游戏对象,并且在进行缩放后,游戏对象应该无法再次放大。目前我的代码能够扩展,但这不是我想要的。 这是我当前的代码:

public void ScaleForRuler()
{       
    transform.localScale += new Vector3 (3.0F, 0, 0.1F);

}       
void OnMouseDown()
{
    if (touch == false) 
    {
        if(ruler.GetComponent<Collider>().name == "Ruler")
        {
            ScaleForRuler ();
            touch = true;
            Debug.Log (touch);
        }
        if(TriangleThingy.GetComponent<Collider>().name == "Triangle_Set_Square_Ruler")
        {
            ScaleForTriangleThingy ();
            touch = true;
            Debug.Log (touch);
        }
         if (Tsquare.GetComponent<Collider> ().name == "Tsquare") 
        {               
            if (LineR.GetComponent<Collider> ().name == "LineRight" || LineL.GetComponent<Collider> ().name == "LineLeft") 
            {
                TsquareScaleForTB ();
                touch = true;
                Debug.Log (touch);
            } 
            else if (LineB.GetComponent<Collider> ().name == "LineBottom" || LineT.GetComponent<Collider> ().name == "LineTop") 
            {
                TsquareScaleForTB ();
                touch = true;
                Debug.Log (touch);
            }                   
        }
        if (protractor.GetComponent<Collider> ().name == "Protractor") 
        {
            ScaleForProtractor ();
            touch = true;
            Debug.Log (touch);
            Debug.Log ("protractor scale");
        }
    }
}   

在对您的问题的评论中,您写道

So i wanted to ask if i could just use a single script to control scaling for many diff objects

答案是肯定的(这也是一个非常好的主意)

这里有一个关于如何做到这一点的想法:

来自 OnMouseDown 的文档:

OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.

这意味着如果您的脚本包含 OnMouseDown 函数:

private void OnMouseDown()
{

}

您可以将此脚本添加到多个不同的对象。如果单击 object1,将只为 object1 调用该函数。如果单击 object2,将为 object2 调用该函数。所以你不需要检查点击了哪个对象。

您的脚本应如下所示:

private void OnMouseDown()
{
    // touch is a boolean, you don't need to write touch == true, it will do it automatically
    // Also, instead of nesting all the code if touch == false, we just stop if it is true, so the code is easier to read
    if (touch)
        return; 

    touch = true;
    Debug.Log (touch);

    // transform is the transform of the object that has the script
    // you don't need any check
    transform.localScale += new Vector3 (3.0f, 0f, 0.1f);  
}   

希望对您有所帮助

祝你好运

停止使用 OnMouseDown(),Unity 在 EventSystem class 中引入了一些新的处理程序接口,例如 IPointerDownHandlerIDragHandler 等, 它与鼠标和触摸输入完美配合。

如果您想在单击每个对象时分别缩放它,或者无论单击哪个对象都一起缩放,您并不清楚您想要实现什么。

我假设您希望能够单独缩放所有对象。执行此操作的步骤如下:

1) 将 Physics 2D Raycaster 组件添加到您的场景相机。

2) 对于要缩放的每个对象,添加这个简单的脚本:

using UnityEngine;
using UnityEngine.EventSystems;

public class ScaleExample : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {

    private Vector2 startPosition;
    private float scalingFactor = .01f;
    private bool isObjectAlreadyScaled = false;

    public void OnBeginDrag(PointerEventData eventData) {
        startPosition = Vector2.zero;
    }

    public void OnDrag(PointerEventData eventData) {
        if (!isObjectAlreadyScaled) {
            startPosition += eventData.delta;
            var scalingAmount = new Vector2(Mathf.Abs(startPosition.x), Mathf.Abs(startPosition.y));
            transform.localScale = (Vector3)scalingAmount * scalingFactor;
        }
    }

    public void OnEndDrag(PointerEventData eventData) {
        isObjectAlreadyScaled = true;
    }
}

该示例适用于 2D 对象(即:放大对象的 X 和 Y),您可以很容易地更改它以适应不同的坐标缩放。 接口的使用应该通过示例清楚:OnBeginDrag在拖动开始时调用一次,OnDrag在每次拖动过程中指针位置发生变化时重复调用,并且OnEndDrag 在拖动结束后立即调用(即:用户释放 button/finger)。