【问题标题】:Unity how to scale gameobjects using mouseclick?Unity如何使用鼠标点击缩放游戏对象?
【发布时间】:2018-02-13 07:36:57
【问题描述】:

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

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");
        }
    }
}   

【问题讨论】:

  • 请解释为什么你的代码不是你想要的?
  • 什么是“尺子”?另外,“它使用其他游戏对象的值以及标尺的值”是什么意思?
  • 放大值,例如当你向上拖动标尺时,标尺确实放大了,但它使用了另一个游戏对象的值以及标尺的值。这是在为公共游戏对象放入游戏对象之后发生的,但是在删除除某个游戏对象之外的所有对象之后,它会给出未分配的引用错误。所以我想问我是否可以只使用一个脚本来控制许多差异对象的缩放。这些值是假定其他游戏对象使用的值。
  • 如您所见,我已经为 4 个游戏对象添加了 debug.log,但由于某种原因,控制台显示了所有 4 个 debug.log 消息。

标签: c# unity3d scaling gameobject


【解决方案1】:

停止使用OnMouseDown(),Unity 不久前在EventSystem 类中引入了一些新的处理程序接口,如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是拖动结束后立即调用(即:用户释放按钮/手指)。

【讨论】:

  • 哦,真的很感谢您的帮助,但我设法找到了使用当前脚本的方法。由于目前我正在使用资产商店中的触摸脚本,我计划在他们的转换手势脚本上使用转换完成。
【解决方案2】:

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

所以我想问我是否可以只使用一个脚本来控制许多差异对象的缩放

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

这是一个关于如何做的想法:

来自OnMouseDown的文档:

当用户在 GUIElement 或 Collider 上按下鼠标按钮时调用 OnMouseDown。

这意味着如果您的脚本包含 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);  
}   

希望对你有帮助

祝你好运

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多