触摸时更改颜色,脚本

时间:2014-07-31 18:28:08

标签: c# unityscript

您好我从youtube的Sebastian教程中获取了此脚本,它会在用户触摸时更改多维数据集的颜色。我需要5个立方体,如果其中一个被触摸,所有立方体都会改变相同的颜色。欢迎任何帮助。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {

public LayerMask touchInputMask;

private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;


void Update () {


    if (Input.touchCount > 0){

        touchesOld = new GameObject[touchList.Count];
        touchList.CopyTo(touchesOld);
        touchList.Clear();

        foreach (Touch touch in Input.touches) {

            Ray ray = camera.ScreenPointToRay(touch.position);      

            if (Physics.Raycast(ray, out hit,touchInputMask)) {

                GameObject recipient = hit.transform.gameObject;
                touchList.Add(recipient);

                if (touch.phase == TouchPhase.Began) {
                    recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Ended) {
                    recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Stationary) {
                    recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
                }
                if (touch.phase == TouchPhase.Canceled) {
                    recipient.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
                }

            }

        }
        foreach (GameObject g in touchesOld) {
            if (!touchList.Contains(g)) {
                g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
            }
        }
    }
    }
}

1 个答案:

答案 0 :(得分:-2)

在脚本末尾再添加两个{{

相关问题