유니티 C# and Shader Study <Basics 01 _ MathMatics Surface> static, enum, delegate
programming/c# 2019. 12. 17. 22:09static함수?
외부에서 접근 가능한 함수로 만들 때 사용함.
using UnityEngine class처럼 다른 스크립트에서 접근해서 사용 가능함.
Delegate ?
함수에 대한 참조. 델리게이트를 사용하면 함수를 변수처럼 사용할 수 있다.
delegate를 사용하기 위해서는 delegate라고 선언하고 return값의 자료형과 파라미터를 입력해주어야 한다. 그 후에 함수를 변수처럼 가져와 사용가능함. 배열에 담아서 사용할 수도 있다.
Enum?
열거형. list로 키워드를 선택할 수 있도록 함.
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Graph : MonoBehaviour { public Transform pointPrefab; [Range(10, 100)] public int resolution = 10; //prefab의 개수 [Range(0.1f, 5)] public float height = .5f; [Range(1, 50)] public float length = 2f; [Range(0, 100)] public float rotationSpeed = 50f; [Range(.1f, 2f)] public float prefabScale = 1f; public GraphName function; //열거형(enum) 형태 private Transform[] points; private void Awake() { points = new Transform[resolution]; for(int i=0; i < points.Length; i++) { Transform point = Instantiate(pointPrefab); point.SetParent(transform, false); points[i] = point; } } private void Update() { float step = length / resolution; Vector3 scale = Vector3.one * prefabScale; float t = Time.time; //delegate를 통한 함수의 배열 가져오기 GraphFuction[] functions = { SineFunction, MultiSineFunction }; //delegate 함수 중 하나 선택 GraphFuction f = functions[(int)function]; for (int i=0; i<points.Length; i++) { //point wave animation Transform point = points[i]; Vector3 position = point.localPosition; position.x = (i + 0.5f) * step - (length / 2); position.y = f(position.x, t); position.z = 0; point.localPosition = position; point.localScale = scale; } //x축으로 회전 Quaternion rotation = transform.localRotation; rotation = Quaternion.Euler(t * rotationSpeed, 0, 0); transform.localRotation = rotation; } public static float SineFunction(float x, float t)//static을 다른 스크립트에서 접근 가능 { return Mathf.Sin(Mathf.PI * (x + t)); } public static float MultiSineFunction(float x, float t) { float y = Mathf.Sin(Mathf.PI * (x + t)); y += Mathf.Sin(2f * Mathf.PI * (x + 0.5f * t))/2f; y *= 2f / 3f; return y; } } | cs |
1 2 3 4 | using UnityEngine; public delegate float GraphFuction(float x, float t); | cs |
1 2 3 4 5 6 7 8 9 | using UnityEngine; public enum GraphName { Sine, MultiSine } | cs |
'programming > c#' 카테고리의 다른 글
GetPixels (0) | 2020.01.01 |
---|---|
유니티 C# and Shader Study <Basics 01 _ Fractal> (0) | 2019.12.28 |
유니티 C# and Shader Study <Basics 01 _ Building a Graph> 지렁이 만들기 (0) | 2019.12.17 |
유니티 C# and Shader Study <Basics 01 _ GameObjects and Scripts> (0) | 2019.12.16 |
Mathf (0) | 2018.11.17 |