【问题标题】:How to translate OnMouse events to Touch phases for Android?如何将 OnMouse 事件转换为 Android 的触摸阶段?
【发布时间】:2014-09-19 00:11:30
【问题描述】:

我坚持使用一个相当简单的解决方案,我的脚本翻译后在 Android 中使用 Unity3d 运行。我有一个游戏对象“Cube”和一个用于旋转的 js 脚本。

我还有一个附加到 GUI.Texture 的脚本“ClickButton.js”。一切正常在 Unity Player 中,但我想翻译此脚本以供 Android 设备中的触摸使用。问题是我做不到,虽然我已经阅读了 Unity 文档。

这里是sn-p的代码:

//This script is attached on a GUI.Texture acting as a button

var normalTexture : Texture2D;
var hoverTexture : Texture2D;

function Update(){
    for (var i = 0; i < Input.touchCount; ++i) {
        if (Input.GetTouch(i).phase == TouchPhase.Began)
            var rotate = Input.GetTouch(i);
            rotate == doRotate();
        }
    }
}

function OnMouseEnter(){
    guiTexture.texture = hoverTexture;
}

function OnMouseExit(){
    guiTexture.texture = normalTexture;
}

function OnMouseDown(){  
    var runScriptRotate : GameObject[] = GameObject.FindGameObjectsWithTag("Marker");    
    for(var doRotation : GameObject in runScriptRotate){    
        var scriptRT : doRotate = doRotation.GetComponent(doRotate);    
        if(scriptRT){
            // access the function "doRotation" on a script named "doRotate" on     gameObject "Cube"
            doRotate.doRotation();
        }    
    }      
}

有人可以编辑此代码脚本,以便通过触摸使其在 Android 上运行吗?提前谢谢大家!

【问题讨论】:

    标签: android button unity3d touch unityscript


    【解决方案1】:

    你为什么不直接复制OnMouseDown()的内容呢?基本上触摸与安卓设备上的鼠标按下是一样的,不是吗?

    if(Input.touchCount > 0){
        if(Input.GetTouch(0).phase == TouchPhase.Began){
            var runScriptRotate : GameObject[] = GameObject.FindGameObjectsWithTag("Marker");    
            for(var doRotation : GameObject in runScriptRotate){    
                var scriptRT : doRotate = doRotation.GetComponent(doRotate);    
                if(scriptRT){
                    // access the function "doRotation" on a script named "doRotate" on gameObject "Cube"
                    doRotate.doRotation();
                }    
            }     
        } 
    }
    

    【讨论】:

    • 嗨@JayKazama,感谢您的回复,但没有成功。我在编译器中收到此错误“错误 BCE0019:'getTouch' 不是 'UnityEngine.Input' 的成员”。有什么解决办法吗?
    • 抱歉,应该是GetTouch,大写G,还有.phase,而不是.touchPhase。我已经编辑了我的答案。试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2016-08-20
    • 1970-01-01
    相关资源
    最近更新 更多