Snippets

ömer faruk sayılır Shining Effect

Created by ömer faruk sayılır last modified
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ShineEffect : MaskableGraphic {

	[SerializeField]
	float yoffset = -1;

	public float Yoffset {
		get {
			return yoffset;
		}
		set {
			SetVerticesDirty();
			yoffset = value;
		}
	}

	[SerializeField]
	float width = 1;

	public float Width {
		get {
			return width;
		}
		set {
			SetAllDirty();
			width = value;
		}
	}

	protected override void OnPopulateMesh (VertexHelper vh)
	{
		var r = GetPixelAdjustedRect();
		var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);
		float dif = (v.w-v.y)*2;
		Color32 color32 = color;
		vh.Clear();

		color32.a = (byte)0;
		vh.AddVert(new Vector3(v.x-50, width*v.y+yoffset*dif), color32, new Vector2(0f, 0f));
		vh.AddVert(new Vector3(v.z+50, width*v.y+yoffset*dif),  color32, new Vector2(1f, 0f));

		color32.a = (byte)(color.a*255);
		vh.AddVert(new Vector3(v.x-50, width*(v.y/4)+yoffset*dif), color32, new Vector2(0f, 1f));
		vh.AddVert(new Vector3(v.z+50, width*(v.y/4)+yoffset*dif), color32, new Vector2(1f, 1f));

		color32.a = (byte)(color.a*255);
		vh.AddVert(new Vector3(v.x-50, width*(v.w/4)+yoffset*dif), color32, new Vector2(0f, 1f));
		vh.AddVert(new Vector3(v.z+50, width*(v.w/4)+yoffset*dif), color32, new Vector2(1f, 1f));
		color32.a = (byte)(color.a*255);

		color32.a = (byte)0;
		vh.AddVert(new Vector3(v.x-50, width*v.w+yoffset*dif), color32, new Vector2(0f, 1f));
		vh.AddVert(new Vector3(v.z+50, width*v.w+yoffset*dif), color32, new Vector2(1f, 1f));

		vh.AddTriangle(0, 1, 2);
		vh.AddTriangle(2, 3, 1);

		vh.AddTriangle(2,3,4);
		vh.AddTriangle(4,5,3);

		vh.AddTriangle(4,5,6);
		vh.AddTriangle(6,7,5);
	}

	public void Triangulate(VertexHelper vh) {
		int triangleCount = vh.currentVertCount - 2;
		Debug.Log(triangleCount);
		for (int i = 0; i <= triangleCount/2+1; i+=2) {
			vh.AddTriangle(i, i+1, i+2);
			vh.AddTriangle(i+2, i+3, i+1);
		}
	}

	#if UNITY_EDITOR
	public override void OnRebuildRequested ()
	{
		base.OnRebuildRequested ();
	}
	#endif

}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[ExecuteInEditMode,RequireComponent(typeof(Image))]
public class ShineEffector : MonoBehaviour {

	public ShineEffect effector;
	[SerializeField,HideInInspector]
	GameObject effectRoot;
	[Range(-1,1)]
	public float yOffset = -1;

	public float YOffset {
		get {
			return yOffset;
		}
		set {
			ChangeVal(value);
			yOffset = value;
		}
	}

	[Range(0.1f,1)]
	public float width = 0.5f;
	RectTransform effectorRect;
	void OnEnable() {
		if (effector == null) {
			GameObject effectorobj = new GameObject("effector");

			effectRoot = new GameObject("ShineEffect");
			effectRoot.transform.SetParent(this.transform);
			effectRoot.AddComponent<Image>().sprite = gameObject.GetComponent<Image>().sprite;
			effectRoot.GetComponent<Image>().type = gameObject.GetComponent<Image>().type;
			effectRoot.AddComponent<Mask>().showMaskGraphic = false;
			effectRoot.transform.localScale = Vector3.one;
			effectRoot.GetComponent<RectTransform>().anchoredPosition3D = Vector3.zero;
			effectRoot.GetComponent<RectTransform>().anchorMax = Vector2.one;
			effectRoot.GetComponent<RectTransform>().anchorMin = Vector2.zero;
			effectRoot.GetComponent<RectTransform>().offsetMax = Vector2.zero;
			effectRoot.GetComponent<RectTransform>().offsetMin = Vector2.zero;
			effectRoot.transform.SetAsFirstSibling();

			effectorobj.AddComponent<RectTransform>();
			effectorobj.transform.SetParent(effectRoot.transform);
			effectorRect = effectorobj.GetComponent<RectTransform>();
			effectorRect.localScale = Vector3.one;
			effectorRect.anchoredPosition3D = Vector3.zero;

			effectorRect.gameObject.AddComponent<ShineEffect>();
			effectorRect.anchorMax = Vector2.one;
			effectorRect.anchorMin = Vector2.zero;

			effectorRect.Rotate(0,0,-8);
			effector = effectorobj.GetComponent<ShineEffect>();
			effectorRect.offsetMax = Vector2.zero;
			effectorRect.offsetMin = Vector2.zero;
			OnValidate();
		}	
	}

	void OnValidate() {
		effector.Yoffset = yOffset;
		effector.Width = width;

		if (yOffset <= -1 || yOffset >= 1) {
			effectRoot.SetActive(false);
		} else if (!effectRoot.activeSelf) {
			effectRoot.SetActive(true);
		} {

		}
	}

	void ChangeVal(float value) {
		effector.Yoffset = value;
		if (value <= -1 || value >= 1) {
			effectRoot.SetActive(false);
		} else if (!effectRoot.activeSelf) {
			effectRoot.SetActive(true);
		} {
			
		}
	}
	void OnDestroy() {
		if ( !Application.isPlaying )
		{
			DestroyImmediate(effectRoot);
		} else {
			Destroy(effectRoot);
		}
	}

}

Comments (0)