SelectionBox

From Unify Community Wiki
Revision as of 21:56, 17 November 2016 by Ivo van der Marel (Talk | contribs)

Jump to: navigation, search

Description

An RTS style selection system, allowing to the player to single click or use a selectionbox to select one or multiple objects

Usage

Every object that needs to be selectable requires at least a collider on itself or one of its children, as well as a Selectable component. The RTSSelection script can be added anywhere in the scene but need to be present somewhere. When adding the RTSSelection script to an empty GameObject you can see some more fields that can be set in the inspector

/*
 * Copyright (c) 2016, Ivo van der Marel
 * Released under MIT License (= free to be used for anything)
 * Enjoy :)
 */
 
using UnityEngine;
 
public class Selectable : MonoBehaviour
{
 
    internal bool isSelected
    {
        get
        {
            return _isSelected;
        }
        set
        {
            _isSelected = value;
            //Replace this with your custom code. What do you want to happen to a Selectable when it get's (de)selected?
            Renderer r = GetComponentInChildren<Renderer>();
            if (r != null)
                r.material.color = value ? Color.red : Color.white;
        }
    }
 
    private bool _isSelected;
 
    void OnEnable()
    {
        RTSSelection.selectables.Add(this);
    }
 
    void OnDisable()
    {
        RTSSelection.selectables.Remove(this);
    }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox