c# - 统一: Android touch on Gui texture

标签 c# mono unity3d

使用 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!!!
}

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

关于c# - 统一: Android touch on Gui texture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19746614/

相关文章:

c# - 类型转换泛型

c# - 为什么这种碰撞检测方法没有击中所有物体?

c# - 在 C# 中运行 Linux 控制台命令

c# - 在现有 C# 代码中发现的 Dispose。我应该摆脱它吗?

c# - 从左到右读取对象

c# - `BatchStatement` 偶尔获取数据不同步

c# - VSTO Excel - 如何创建包含多个区域的范围

c# - 更新后 Insights 中的 Xamarin iOS 对象处置异常

c# - Mono 2.8.1 断言 mono_wsq_count (wsq) == 0

c# - 寻找增强算法 : Save game state