다음은 내가 사용하는 해결책입니다. Line Renderer를 사용하면 마녀가 마침내 Unity 5.5에서 beatifull이됩니다.
버튼으로 사용하는 객체가 있습니다. 그것은 LineRenderer와이 스크립트 인 PoligonCollider2D를 가지고 있습니다. 또한 라인 렌더러는 해상도가 변경된 후에 자체를 다시 그려야합니다. 그래서 특정 해상도로 내 Poligon 콜리더를 구성합니다.이 해상도는 게임 창에서 구성 할 수 있습니다. 내 스크립트에서는 689 * 500, 즉 16/9 해상도를 사용합니다. 캔버스는 화면 공간 - 카메라이며 가로 세로 비가 1.7777778입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PolygonButton : MonoBehaviour {
public string param;
public System.Action onClick;
float animSpeed = 1.5f;
IEnumerator anim;
bool animating;
Rect res;
Camera mainCam;
float theWidth = 689;
void Start()
{
mainCam = GameObject.FindObjectOfType<Camera>();
res = mainCam.pixelRect;
anim = buttonAnimation();
animating = false;
GetComponent<LineRenderer>().material.SetColor("_Color", new Color32(255, 255, 0, 0));
LineRenderer lr = GetComponent<LineRenderer>();
PolygonCollider2D polColliedr = GetComponent<PolygonCollider2D>();
int i = 0;
lr.numPositions = polColliedr.points.Length + 1;
foreach (Vector2 point in polColliedr.points)
{
lr.SetPosition(i, point);
i++;
}
lr.SetPosition(i, lr.GetPosition(0));
//change scale for different aspect ratio
float currWidth = GetComponentInParent<Canvas>().GetComponent<RectTransform>().sizeDelta.x;
GetComponent<RectTransform>().localScale = new Vector3(currWidth/theWidth, currWidth/theWidth, currWidth/theWidth);
}
void Update()
{
//If resolution changes - we must change button scale
if(mainCam.pixelRect.height != res.height || mainCam.pixelRect.width != res.width)
{
res = mainCam.pixelRect;
float currWidth = GetComponentInParent<Canvas>().GetComponent<RectTransform>().sizeDelta.x;
GetComponent<RectTransform>().localScale = new Vector3(currWidth/theWidth, currWidth/theWidth, currWidth/theWidth);
}
}
void OnMouseEnter()
{
CoroutineExecutor.instance.Execute(anim);
}
void OnMouseExit()
{
ResetAnim();
}
void OnMouseUpAsButton()
{
ResetAnim();
if(onClick != null)
onClick();
}
void OnDestroy()
{
CoroutineExecutor.instance.StopExecution(anim);
}
void ResetAnim()
{
CoroutineExecutor.instance.StopExecution(anim);
anim = null;
anim = buttonAnimation();
GetComponent<LineRenderer>().material.SetColor("_Color", new Color32(255, 255, 0, 0));
animating = false;
}
IEnumerator buttonAnimation()
{
//Debug.Log("Start animation!");
if (animating)
yield break;
GetComponent<LineRenderer>().material.SetColor("_Color", new Color32(255, 255, 0, 0));
animating = true;
LineRenderer lr = GetComponent<LineRenderer>();
while (true)
{
float t = 0;
while(t < 1)
{
t += Time.deltaTime * animSpeed;
lr.material.SetColor("_Color", Color.Lerp(new Color32(255, 255, 0, 0), new Color32(255, 255, 0, 255), t));
yield return new WaitForEndOfFrame();
}
t = 0;
while (t < 1)
{
t += Time.deltaTime * animSpeed;
lr.material.SetColor("_Color", Color.Lerp(new Color32(255, 255, 0, 255), new Color32(255, 255, 0, 0), t));
yield return new WaitForEndOfFrame();
}
yield return new WaitForEndOfFrame();
}
}
}
그게 전부는 다음과 같습니다
Example
이 쉽게 C#을 수행 할 수 있습니다. 포토샵이 필요하지 않습니다. 이 사진이 실제로 보일 것 같은 사진을 업로드하여 내가 찾고있는 사진인지 확인할 수 있습니까? – Programmer
안녕하세요. 다음과 같이 보입니다. https://gyazo.com/53d76b924420d6b9294ba9aa2a3445c7 Photoshop으로 어떻게했는지는 비슷하지만 비슷한 효과가 나타납니다! – harrisoncrazy