using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DetectClicksAndTouches : MonoBehaviour
{
//This variable is optional; if not set it will default to the main camera
//This is so that you can detect clicks/touches on a separate UI Camera
//This variable does NOT update in real time
public Camera detectionCamera;
//This variable adds a Debug.Log call to show what was touched
public bool debug = false;
//This is the actual camera we reference in the update loop, set in Start()
private Camera _camera;
//List<GameObject> UnitPlayerBase = new List<GameObject>();
void Start()
{
if(detectionCamera != null)
{
_camera = detectionCamera;
}
else
{
_camera = Camera.main;
}
}
// Update is called once per frame
void Update ()
{
Ray ray;
RaycastHit hit;
if(Input.GetMouseButtonDown(0)) //Check to see if we've clicked
{
//Set up our ray from screen to scene
ray = _camera.ScreenPointToRay(Input.mousePosition);
//If we hit...
if(Physics.Raycast (ray, out hit, Mathf.Infinity))
{
//Tell the system what we clicked something if in debug
if(debug)
{
Debug.Log("You clicked " + hit.collider.gameObject.name,hit.collider.gameObject);
}
if (hit.collider.tag == "Player")
{
UnitBase.temp.UnitPlayerBaseFunction(hit.collider.gameObject);
//UnitPlayerBase.Add(hit.collider.gameObject);
//Debug.Log(UnitPlayerBase.Count);
}
//Run the Clicked() function on the clicked object
}
hit.transform.gameObject.SendMessage("Clicked", hit.point, SendMessageOptions.DontRequireReceiver);
}
if(Input.GetMouseButtonDown(1)) //Check to see if we've clicked
{
//Set up our ray from screen to scene
ray = _camera.ScreenPointToRay(Input.mousePosition);
//If we hit...
if(Physics.Raycast (ray, out hit, Mathf.Infinity))
{
//Tell the system what we clicked something if in debug
if(debug)
{
Debug.Log("You right clicked " + hit.collider.gameObject.name,hit.collider.gameObject);
}
//Run the Clicked() function on the clicked object
hit.transform.gameObject.SendMessage("RightClicked", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}[Добавлено (11.07.2014, 12:56)
---------------------------------------------
это первый файл вот второй (чуть код сменил)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UnitBase : MonoBehaviour
{
public static UnitBase temp;
List<GameObject> UnitPlayerBase = new List<GameObject>();
public void UnitPlayerBaseFunction(GameObject g)
{
UnitPlayerBase.Add(g);
Debug.Log(UnitPlayerBase.Count);
}
}