Unity3d+moba+ Skill Indicator (ii)

Source: Internet
Author: User
Tags cos mul sin

2.3 indicator picture highlighting shader

Create a new shaderwith the following code

Shader "CUSTOM/SKILLHINTBG" {Properties {_maintex ("Base Texture", 2D) = "White" {}_color ("Main Color", Color) = (1,1,1,1 )}category {Tags {"Queue" = "Transparent" "ignoreprojector" = "True" "rendertype" = "Transparent"}blend Srcalpha Oneminussrcalphacull off Lighting off Zwrite off bindchannels {Bind "Color", Colorbind "Vertex", Vertexbind "Texcoord", TE Xcoord}subshader {Pass {//ztest alwayscgprogram #pragma vertex vert #pragma fragment frag#include "Unitycg.cginc" str   UCT v2f {float4 pos:sv_position;   FLOAT2 uv:texcoord0;   };   v2f Vert (Appdata_tan v) {v2f o;  O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.UV = V.texcoord;    return o;  } sampler2d _maintex; FLOAT4 _color;    Half4 Frag (v2f i): COLOR {half4 result = tex2d (_maintex, I.UV); Result*=_color;return result;}   ENDCG}pass {ZTest greatercgprogram #pragma vertex vert #pragma fragment frag#include "unitycg.cginc" struct v2f {   FLOAT4 pos:sv_position;   FLOAT2 uv:texcoord0;   }; v2f Vert (AppData_tan v) {v2f o;  O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.UV = V.texcoord;    return o;  } sampler2d _maintex; FLOAT4 _color;    Half4 Frag (v2f i): COLOR {half4 result = tex2d (_maintex, I.UV); result*=_color;result.a/= 3;return result;} ENDCG}}}}

The original used shader


The effect of formation


after using the highlight shader


2.4Sector Range Indicator

This is to generate a fan meshdynamically, by setting the angle and radius to the effect you want to display. The dynamic generation of sector mesh code is as follows:

Using unityengine;using System.Collections;  [Requirecomponent (typeof (Meshrenderer), typeof (Meshfilter))]public class meshfan:monobehaviour{public float radius =    2;    public float angledegree = 100;    public int segments = 10;    public int angledegreeprecision = 1000;    public int radiusprecision = 1000;    Private Meshfilter Meshfilter;    Private Sectormeshcreator Creator = new Sectormeshcreator ();    Private Mesh M_mesh;    [Executeineditmode] private void Awake () {meshfilter = getcomponent<meshfilter> (); private void Update () {Meshfilter.mesh = Creator.    Createmesh (RADIUS, Angledegree, segments, angledegreeprecision, radiusprecision);        } void Ondrawgizmos () {gizmos.color = Color.gray;    DrawMesh ();        } void Ondrawgizmosselected () {gizmos.color = Color.green;    DrawMesh (); private void DrawMesh () {Mesh mesh = Creator. Createmesh (RADIUS, Angledegree, segments, Angledegreeprecision, Radiusprecision);        Int[] Tris = mesh.triangles; for (int i = 0; i < Tris. Length; i + = 3) {gizmos.drawline (Convert2world (Mesh.vertices[tris[i]), Convert2world (Mesh.vertices[tris[i + 1]            ]));            Gizmos.drawline (Convert2world (Mesh.vertices[tris[i]]), Convert2world (Mesh.vertices[tris[i + 2]));        Gizmos.drawline (Convert2world (mesh.vertices[tris[i + 1]]), Convert2world (Mesh.vertices[tris[i + 2])); }} private Vector3 Convert2world (Vector3 src) {return transform.    Transformpoint (SRC);        } private class Sectormeshcreator {private float radius;        private float Angledegree;        private int segments;        Private Mesh Cachemesh; <summary>//Create a sector mesh///</summary>//<param name= "radius" > Sector half Price </param >//<param name= "Angledegree" > Fan angle </param>//<param Name= "segments" > Sector arc segment Number </par Am>//<param name= "angledegreeprecision" > Fan angle accuracy (within the accuracy range, considered to be the same angle) </param>//<param name= "Radiusprecision" &G        T        <pre>///sector half-price accuracy (is considered to be the same half-price at half-price accuracy range). For example: half-price accuracy is 1000, then: 1.001 and 1.002 are not considered to be the same radius.        Because they are not equal after 1000 times times magnification.        If the half-price accuracy is set to 100, then 1.001 and 1.002 can be considered equal. </pre>//</param>/<returns></returns> public Mesh Createmesh (float Radius, float angledegree, int segments, int angledegreeprecision, int radiusprecision) {if (Checkdiff ( Radius, Angledegree, segments, Angledegreeprecision, radiusprecision)) {Mesh Newmesh = Create (r                Adius, Angledegree, segments);                    if (Newmesh! = null) {Cachemesh = Newmesh;                    This.radius = radius;                    This.angledegree = Angledegree;                This.segments = segments;        }} return Cachemesh;    }    Private Mesh Create (float radius, float angledegree, int segments) {if (segments = = 0) { segments = 1; #if unity_editor Debug.Log ("segments must be larger than zero.");            #endif} Mesh mesh = new mesh ();            vector3[] vertices = new Vector3[3 + segments-1];//all vertex data vertices[0] = new Vector3 (0, 0, 0);            float angle = Mathf.deg2rad * ANGLEDEGREE;            float currangle = ANGLE/2;            float deltaangle = angle/segments;            Currangle = Mathf.deg2rad * (+ ANGLEDEGREE/2); Generate vertex data for (int i = 1; i < vertices. Length; i++) {Vertices[i] = new Vector3 (Mathf.cos (currangle) * radius, 0, Mathf.sin (currangle) * radius                );            Currangle-= Deltaangle;  }//Generate triangle data int[] triangles = new Int[segments * 3];//has segments triangles, each of 3 data forms a triangle for (int i = 0, vi = 1; I < Triangles. Length;                i + = 3, vi++) {triangles[i] = 0;                Triangles[i + 1] = VI;            Triangles[i + 2] = vi + 1;            } mesh.vertices = vertices;            Mesh.triangles = triangles; Texture coordinate vector2[] Uvs = new vector2[vertices.            Length]; for (int i = 0; i < Uvs. Length;            i++) {Uvs[i] = new Vector2 (vertices[i].x, vertices[i].z);                        } MESH.UV = Uvs;        return mesh; } private bool Checkdiff (float radius, float angledegree, int segments, int angledegreeprecision, int radiusprecisi On) {return segments! = This.segments | | (int)                   ((angledegree-this.angledegree) * angledegreeprecision)! = 0 | | (int)        ((Radius-this.radius) * radiusprecision)! = 0; }    }}

Mesh: A mesh component that is used primarily to set the shape and appearance. 3d models are made up of N triangles, and mesh is the collection that holds the description information, creating a mesh Grid : Assign a triangle in the following order : 1) assign vertex 2).


Creates a fan-shaped Meshcomposed of ten small triangles. The total number of vertices is . Section 0 Vertex coordinates of ( 0,0,0 ), and then take Z The axes are in the positive direction and each point coordinate is calculated in turn.

Generate vertex data for            (int i = 1; i < vertices. Length; i++)            {                Vertices[i] = new Vector3 (Mathf.cos (currangle) * radius, 0, Mathf.sin (currangle) * radius);                Currangle-= Deltaangle;            }

After all vertex coordinates have been assigned, the triangle is allocated. Mesh.triangles as a int[], The quantity must be 3 the multiple, per 3 To a group, each value is an index number to mesh.vertices to find the corresponding coordinate data in the

Generate triangle Data            int[] triangles = new Int[segments * 3];//has segments triangles, each of 3 data forms a triangle for            (int i = 0, vi = 1; i < TR Iangles. Length; i + = 3, vi++)            {                triangles[i] = 0;                Triangles[i + 1] = VI;                Triangles[i + 2] = vi + 1;            }

Finally, the fan mesh parameters are set according to the cast distance and the influence radius of the skill .


2.5Selective Skill Indicator

This is the same as a fan, that is, you do not need to set the degree, using a default selection of degrees, such as the degree. When there are 1 enemies within this selection fan , he is the target. If more than one enemy is within range, select the closest angle to the center ray. :

1. No enemies within the fan range of the selection indicator


2. When there is an enemy in the fan range of the selection indicator, the selected enemy has a red pillar on its head (later using the light of God)


3. When there are multiple enemies within the selection range, select the closest to the center Ray


The code is as follows:

<summary>///Select Prompt///</summary>//<param name= "skill" ></param>//<param Name= "obj" ></param> public Unitctrl tarselect (Skillctrl skill,gameobject obj) {Unitctrl unitselect        = NULL;        float Fradius = 0.0f;        Fradius = radius of skill.m_disrange;//skill Vector3 pos = transform.position; collider[] Bufcollider = Physics.overlapsphere (pos, Fradius);//Get around members list<unitctrl> Listunit = new List<U        Nitctrl> (); foreach (var item in Bufcollider) {Unitctrl unit = Item.            Getcomponentinparent<unitctrl> (); if (unit! = null) {if (!                    MainMgr.self.isWe (unit.m_camp) && unit.isalive && unit.m_isvisible)//Non-us, alive, visible {                Listunit.add (unit);        }}} float mindegree = m_selectdegree/2.0f; Re-filtering within a large circle 1. Meet the selection angle, 2. The minimum angle to the center ray of the foreach (Var uNit in Listunit) {Vector3 Unitvec = unit.transform.position-obj.transform.position;            Vector3 Selectvec = Obj.transform.forward;            float degree = vector3.angle (Unitvec, Selectvec);                if (degree <= mindegree) {mindegree = degree;            Unitselect = unit;        }} if (Unitselect! = null) {UIMgr.self.headLockSet (true, unitselect);        } else {UIMgr.self.headLockSet (false, Unitselect); } return unitselect;}

It should be noted that the selected red pillar, the scene can only have one (select medium-sized skills can only attack a person at a time), when I have a choice of target, set the parent node of the Red column as the target (to achieve the red pillar in the selected range with the target), when I do not have a selection target, the red pillar does not The child nodes of the pool. The code is as follows:

public void Headlockset (bool Active,unitctrl unit)    {        m_headlock.setactive (active);        if (active = = True)        {            m_headLock.transform.SetParent (unit.transform);            M_headLock.transform.localPosition = new Vector3 (0,10,0);            Renderer hintrenderer = m_headlock.getcomponent<renderer> ();            HintRenderer.material.SetColor ("_color", color.red);        }        else        {            m_headLock.transform.SetParent (m_prefab.transform);            M_headLock.transform.localPosition = new Vector3 (0, 0, 0);        }    }



Unity3d+moba+ Skill Indicator (ii)

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.