gpt4 book ai didi

c# - 统一: Android touch on Gui texture

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:10 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com