Skip to main content
 首页 » 编程设计

c#之统一: Android touch on Gui texture

2024年02月13日19rubylouvre

使用 Unity 4.1.2,使用 C# mono,Android 开发。

大家好,我创建了以下脚本。

using UnityEngine; 
using System.Collections; 
 
public class ButtonLeftMove : MonoBehaviour { 
 
// Use this for initialization 
public float MoveSpeed = 10; 
private float min = -20f; 
private float max = 20f; 
 
void Start () { 
 
} 
 
// Update is called once per frame 
void FixedUpdate ()  
{ 
    MoveSpeed = 50F; 
    GameObject joeblob = GameObject.FindGameObjectWithTag("playertag"); 
 
    if(Input.touches.Length > 0) 
    { 
        Debug.Log ("Touched"); 
        joeblob.rigidbody.AddForce(Vector3.left * MoveSpeed); 
        //rigidbody.AddForce(Vector3.right * MoveSpeed); 
    } 
 
    Vector3 clampVel = joeblob.rigidbody.velocity; 
    clampVel.x = Mathf.Clamp(clampVel.x, min, max); 
 
    joeblob.rigidbody.velocity = clampVel; 
} 
} 

效果很好,触摸屏幕时对象会移动,但是对于屏幕的任何部分都是如此。

此脚本附加到我的 GUI 纹理,我只希望它在触摸 GUI 纹理时为真,而不是在屏幕上的任何地方!

我缺少什么额外的代码?

干杯

请您参考如下方法:

作为 GUIElement 的 GUITexture 有一个名为 HitTest 的方法.您可以使用它来测试您的触摸位置是否在您的 GUITexture 内。这就像

if(guiTexture.HitTest(Input.touches[0].position)) 
{ 
    //I'm hit, I'M HIT!! GOING DOWN!!! 
} 

当然,您必须确保确实有接触,但您明白了。