I’m currently working on a selection system for my first project that is an army control game. With the help of the user “Marrrk” he provided me with a script, which can be seen below
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class SelectionManager
{
public List<Unit> _selection = new List<Unit>();
public Action<Unit> UnitSelected;
public Action<Unit> UnitDeselected;
public void Select(Unit unit)
{
if (_selection.Contains(unit))
return;
_selection.Add(unit);
if (UnitSelected != null)
UnitDeselected(unit);
}
public void Deselect(Unit unit)
{
if (!_selection.Contains(unit))
return;
_selection.Remove(unit);
if (UnitSelected != null)
UnitSelected(unit);
}
public static SelectionManager Default = new SelectionManager();
}
As you can see this Script calls the “Unit” class quite often, which is making me a little unsure of what I’m doing here… He provided me with the code-segmant to select and Deselect units aswell as check to see if they were selected
To Select a unit
SelectionManager.Default.Select(myUnit);
and to Deselect a unit
SelectionManager.Default.Deselect(myUnit);
Although, I have no idea how to pass the parameter of myUnit, because I don’t know anything about the Unit class, and I can’t find it in the scripting reference.
So here if my (poorly coded) selectable.cs script which is attatched to anything that can be selected, can you guys help me implement this?
using UnityEngine;
using System.Collections;
public class Selectable : MonoBehaviour {
GUIManager guiManager;
public bool unit = true;
public bool selected = false;
public Transform selector;
// Use this for initialization
void Start () {
guiManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GUIManager>();
if(selector != null)
selector.renderer.enabled = false;
}
void Update () {
if(selector != null) {
if(selected)
selector.renderer.enabled = true;
else
selector.renderer.enabled = false;
}
if(!unit) {
} else {
if(renderer.isVisible Input.GetMouseButton (0)) {
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = MouseManager.screenToRectSpace(camPos.y);
if(guiManager.isHoverGUI)
return;
selected = MouseManager.selectionRect.Contains (camPos);
}
}
}
}
Thank you, and major thanks to marrrk for taking the time to write the original script out for me and explain its uses.