1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | using UnityEngine; using System.Collections; public class TextOutline : MonoBehaviour { //두께 설정 public float pixelSize = 1; public Color outlineColor = Color.black; //해상도에 따라 pixel size를 조절할지 결정 public bool resolutionDependant = false; //설정된 Resolution보다 클 경우 pixel size 두배로 결정 public int doubleResolution = 1024; private TextMesh textMesh; private MeshRenderer meshRenderer; private bool isDead = false; // Use this for initialization void Start () { isDead = false; textMesh = GetComponent<TextMesh>(); meshRenderer = GetComponent<MeshRenderer>(); for(int i = 0; i<8; i++) { GameObject outline = new GameObject("outline", typeof(TextMesh)); outline.transform.parent = transform; outline.transform.localScale = new Vector3(1, 1, 1); MeshRenderer otherMeshrenderer = outline.GetComponent<MeshRenderer>(); otherMeshrenderer.material = new Material(meshRenderer.material); otherMeshrenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; otherMeshrenderer.receiveShadows = false; otherMeshrenderer.sortingLayerID = meshRenderer.sortingLayerID; otherMeshrenderer.sortingLayerName = meshRenderer.sortingLayerName; } } // Update is called once per frame void Update () { } public void LateUpdate() { if (isDead) return; Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position); outlineColor.a = textMesh.color.a * textMesh.color.a; for(int i = 0; i<transform.childCount; i++) { TextMesh other = transform.GetChild(i).GetComponent<TextMesh>(); other.color = outlineColor; other.text = textMesh.text; other.alignment = textMesh.alignment; other.characterSize = textMesh.characterSize; other.font = textMesh.font; other.fontSize = textMesh.fontSize; other.fontStyle = textMesh.fontStyle; other.richText = textMesh.richText; other.tabSize = textMesh.tabSize; other.lineSpacing = textMesh.lineSpacing; other.offsetZ = textMesh.offsetZ; bool doublePixel = resolutionDependant && (Screen.width > doubleResolution || Screen.height > doubleResolution); Vector3 pixelOffset = GetOffset(i) * (doublePixel ? 2.0f * pixelSize : pixelSize); Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPoint + pixelOffset); other.transform.position = worldPoint; MeshRenderer otherMeshRenderer = transform.GetChild(i).GetComponent<MeshRenderer>(); otherMeshRenderer.sortingLayerID = meshRenderer.sortingLayerID - 1; otherMeshRenderer.sortingLayerName = meshRenderer.sortingLayerName; } } public void dead() { isDead = true; for(int i = 0; i<transform.childCount; i++) { GameObject other = transform.GetChild(i).gameObject; Destroy(other); } Destroy(this); } Vector3 GetOffset(int i) { switch(i % 8) { case 0: return new Vector3(0, 1, 0); case 1: return new Vector3(1, 1, 0); case 2: return new Vector3(1, 0, 0); case 3: return new Vector3(1, -1, 0); case 4: return new Vector3(0, -1, 0); case 5: return new Vector3(-1, -1, 0); case 6: return new Vector3(-1, 0, 0); case 7: return new Vector3(-1, 1, 0); default: return Vector3.zero; } } } | cs |
'Unity' 카테고리의 다른 글
| Particle System (0) | 2015.04.14 |
|---|---|
| Invoke Coroutine (0) | 2015.04.14 |
| Text 그라데이션 (0) | 2015.04.13 |
| 3D Text 글자 흐림 문제 (0) | 2015.04.13 |
| Unity4.6 이상 Text Shadow outline stroke (0) | 2015.04.10 |