-
[게임엔진활용] 3강 Geometry, Texture, String[DSU] 게임엔진활용 2026. 3. 30. 01:58728x90
1. 더블 버퍼링, 해상도 (RenderView.cs => MainCamera.cs setViewport)
지난 시간 이어 설명 ...
2. OnGUI
123456789101112131415161718192021222324void OnGUI(){if( Event.current.type != EventType.Repaint )return;RenderTexture texBack = RenderTexture.active;RenderTexture.active = mc.rt;Matrix4x4 matBack = GUI.matrix;GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);// draw something ...RenderTexture.active = texBack;GUI.matrix = matBack;GL.Clear(true, true, Color.black);GUI.matrix = Matrix4x4.TRS(new Vector3(mc.viewport.x, mc.viewport.y, 0),Quaternion.identity,new Vector3((float)(Screen.width - mc.viewport.x * 2) / mc.devWidth,(float)(Screen.height - mc.viewport.y * 2) / mc.devHeight, 1.0f));drawImage(mc.rt, 0, 0, 0);}cs 3. Texture
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778public const int TOP = 1, BOTTOM = 2, VCENTER = 4,LEFT = 8, RIGHT = 16, HCENTER = 32;public const int REVERSE_NONE = 0, REVERSE_WIDTH = 1, REVERSE_HEIGHT = 2;Texture createImage(string path){return Resources.Load<Texture>(path);}void drawImage(Texture tex, float x, float y, int anc = TOP | LEFT ){drawImage(tex, x, y, 0, 0, tex.width, tex.height, anc, 1.0f, 1.0f, 2, 0, REVERSE_NONE);}void drawImage(Texture tex, float x, float y,float tx, float ty, float tw, float th, int anc = TOP | LEFT,float rx = 1f, float ry = 1f, int xyz = 2, float degree = 0f, int reverse = REVERSE_NONE){int w = (int)(tw * rx);int h = (int)(th * ry);switch(anc){case TOP | LEFT: break;case TOP | RIGHT: x -= w; break;case TOP | HCENTER: x -= w / 2; break;case BOTTOM | LEFT: y -= h; break;case BOTTOM | RIGHT: x -= w; y -= h; break;case BOTTOM | HCENTER: x -= w / 2; y -= h; break;case VCENTER | LEFT: y -= h / 2; break;case VCENTER | RIGHT: x -= w; y -= h / 2; break;case VCENTER | HCENTER: x -= w / 2; y -= h / 2; break;}Matrix4x4 matrixPrjection = GUI.matrix;Matrix4x4 matrixModelview = Matrix4x4.TRS(new Vector3(x + w / 2, y + h / 2, 0),Quaternion.Euler(xyz == 0 ? degree : 0,xyz == 1 ? degree : 0,xyz == 2 ? degree : 0),Vector3.one);GUI.matrix = GUI.matrix * matrixModelview;if ((reverse & REVERSE_WIDTH) == REVERSE_WIDTH){Matrix4x4 m0 = Matrix4x4.TRS(new Vector3(0, 0, 0),Quaternion.Euler(0, 180, 0),new Vector3(1, 1, 1));GUI.matrix = GUI.matrix * m0;}else if ((reverse & REVERSE_HEIGHT) == REVERSE_HEIGHT){Matrix4x4 m1 = Matrix4x4.TRS(new Vector3(0, 0, 0),Quaternion.Euler(180, 0, 0),new Vector3(1, 1, 1));GUI.matrix = GUI.matrix * m1;}Material m = material[0];m.SetColor("_BaseColor", Color.white);m.SetTexture("_BaseMap", tex);Graphics.DrawTexture(new Rect(-w / 2, -h / 2, w, h),tex,new Rect(tx / tex.width, ty / tex.height, tw / tex.width, th / tex.height),0, 0, 0, 0, m);GUI.matrix = matrixPrjection;}cs 알파 채널을 사용하기 위해, Shader에서 Blend를 설정

4. Line, Rect
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Texture texDot;Color color;float lineWidth;void init(){Texture2D tex = new Texture2D(1, 1);tex.SetPixel(0, 0, Color.white);tex.Apply();texDot = tex;}public void setRGBA(float r, float g, float b, float a){color.r = r;color.g = g;color.b = b;color.a = a;}public void setLineWidth(float width){lineWidth = width;}public void drawLine(float sx, float sy, float ex, float ey){float cx = (sx + ex) / 2;float cy = (sy + ey) / 2;float dx = ex - sx;float dy = ey - sy;float len = Mathf.Sqrt(dx * dx + dy * dy);float degree = Mathf.Atan2(ey - sy, ex - sx) * Mathf.Rad2Deg;drawImage(texDot, cx, cy, 0, 0, 1, 1, VCENTER | HCENTER,len, lineWidth, 2, degree, REVERSE_NONE);}public void drawRect(float x, float y, float width, float height){// top & bottomdrawLine(x, y, x + width, y);drawLine(x, y + height - lineWidth,x + width, y + height - lineWidth);// left & rightdrawLine(x, y + lineWidth, x, y + height - lineWidth);drawLine(x + width - lineWidth, y + lineWidth,x + width - lineWidth, y + height - lineWidth);}public void fillRect(float x, float y, float width, float height){drawImage(texDot, x, y, 0, 0, 1, 1, TOP | LEFT,width, height, 2, 0, REVERSE_NONE);}cs 5. String
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566string stringName = null;float stringSize = 20f;Color stringColor = Color.white;public string getStringName(){return stringName;}public void setStringName(string name){stringName = name;}public float getStringSize(){return stringSize;}public void setStringSize(float size){stringSize = size;}public Color getStringRGBA(){return stringColor;}public void setStringRGBA(float r, float g, float b, float a){stringColor = new Color(r, g, b, a);}public Vector2 sizeOfString(string str){GUIStyle style = new GUIStyle(GUI.skin.label);style.font = Resources.Load<Font>(stringName);style.fontSize = (int)stringSize;style.fontStyle = FontStyle.Normal;style.normal.textColor = stringColor;return style.CalcSize(new GUIContent(str));}public void drawString(string str, float x, float y, int anc = TOP | LEFT){GUIStyle style = new GUIStyle(GUI.skin.label);style.font = Resources.Load<Font>(stringName);style.fontSize = (int)stringSize;style.fontStyle = FontStyle.Normal;style.normal.textColor = stringColor;style.hover.textColor = stringColor;Vector2 size = style.CalcSize(new GUIContent(str));switch (anc){case TOP | LEFT: break;case TOP | RIGHT: x -= size.x; break;case TOP | HCENTER: x -= size.x / 2; break;case BOTTOM | LEFT: y -= size.y; break;case BOTTOM | RIGHT: x -= size.x; y -= size.y; break;case BOTTOM | HCENTER: x -= size.x / 2;y -= size.y; break;case VCENTER | LEFT: y -= size.y / 2;break;case VCENTER | RIGHT: x -= size.x; y -= size.y / 2;break;case VCENTER | HCENTER: x -= size.x / 2;y -= size.y / 2;break;}GUI.color = stringColor;// #issueGUI.Label(new Rect(x, y, size.x, size.y), str, style);}cs 
'[DSU] 게임엔진활용' 카테고리의 다른 글
[게임엔진활용] 5강 AutoNumber, ProgressBar (0) 2026.04.13 [게임엔진활용] 4강 미니(사과)게임 제작 (0) 2026.04.07 [게임엔진활용] 2강 게임 개발 해상도(더블버퍼링, 뷰포트) (0) 2026.03.25 [게임엔진활용] 1-5강 쉐이더토이 적용 (0) 2026.03.16 [게임엔진활용] 1-4강 쉐이더로 효과 만들기 (1) 2026.03.12

