Red police Box selection function

Source: Internet
Author: User

Red police box selection function, my idea here is to use unity inside the GL to achieve, and then put the soldiers in a list set, the soldier's coordinates into screen coordinates, and then determine whether in the range of the box, if within the range, move.

(1) Build a material, the color of the material determines the color of the box, and then the type of material is set to Ui/default.

(2) write the following code

usingUnityengine;usingSystem.Collections;/// <summary>///GL Frame, the script must be mounted on the camera./// </summary> Public classcameragl:monobehaviour{ PublicMaterial Mat; Put the first step on top of the material. Public BOOLShoulddrawrect;  PublicVector2 startpos;  PublicFrameselect FS; voidOnpostrender () {if(shoulddrawrect) {Vector2 Endpos=input.mouseposition; Gl.            Pushmatrix (); Mat. SetPass (0); Gl.            Loadpixelmatrix (); Gl. Begin (GL.            QUADS); Gl.            Vertex (startpos); Gl. Vertex (NewVector3 (Startpos.x, Endpos.y,0)); Gl.            Vertex (Endpos); Gl. Vertex (NewVector3 (Endpos.x, Startpos.y,0)); Gl.            End (); Gl.            Popmatrix ();            Fs.selectSoliders.Clear (); foreach(Solider Minchfs.allsoliders) {Vector3 pos=m.transform.position; Vector3 Temppos= Camera.main.WorldToScreenPoint (POS);//turn world coordinates into screen coordinatesVector3 P1 =startpos; Vector3 P2=Endpos; if(Startpos.x <endpos.x) {p1.x=endpos.x; p2.x=startpos.x; }                if(Startpos.y <endpos.y) {p1.y=Endpos.y; P2.y=Startpos.y; }                if(Temppos.x > p2.x && temppos.x < p1.x && temppos.y > P2.y && temppos.y <p1.y) {m.Select=true;                FS.SELECTSOLIDERS.ADD (m); }            }        }    }    voidUpdate () {if(Input.getmousebuttondown (0) ) {Shoulddrawrect=true; Startpos=input.mouseposition; }        if(Input.getmousebuttonup (0) ) {Shoulddrawrect=false; }    }}

3 Code Box selection control

usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine.ui;/// <summary>///Box Selection Control/// </summary> Public classframeselect:monobehaviour{ PublicList<solider>allsoliders;  PublicList<solider>selectsoliders; voidAwake () {selectsoliders=NewList<solider>(); }    voidUpdate () {if(Input.getkeydown (Keycode.escape)) {foreach(Solider Iteminchselectsoliders) {Item.Select=false;        } selectsoliders.clear (); }    }}

4 Soldier Class
usingUnityengine;usingSystem.Collections;/// <summary>///Each soldier mounts the class/// </summary> Public classsolider:monobehaviour{[Serializefield]Private BOOL_select; [Serializefield]Privatecolor color; voidAwake () {_select=false; Color= getcomponent<meshrenderer>(). Material.color; }     Public BOOL Select    {        Set{_select=value; if(_select) {getcomponent<MeshRenderer> (). Material.color =Color.green; }            Else{getcomponent<MeshRenderer> (). Material.color =color; }        }        Get        {            return_select; }    }}

Probably this is the case, the specific project documents, I hit the package at the address below, you can download, I use the Unity version is 5.1.2f, if the version is low will not open, the version of High may have problems

Http://files.cnblogs.com/files/ZhiXing-Blogs/FrameSelect.rar

Red police Box selection function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.