'programming'에 해당되는 글 96건

  1. 2020.02.19 유니티 colorPulse
  2. 2020.02.19 유니티 textureOffset
  3. 2020.02.15 유니티 line renderer로 그리드 생성
  4. 2020.02.02 유니티 객체 생성하기 (Instantiate)
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class colorPulse : MonoBehaviour {
 
    private Material mat;
    public Color colourEnd;
    public Color colourStart;
    private float i;
    private float randomTime;
 
void Start()
{
    colourEnd = Color.black;
    colourStart = Color.red;
    mat = GetComponent<Renderer>().material;
}
void Update()
{
    randomTime = Random.Range(2.0f, 5.0f);
    i += Time.deltaTime;
    mat.SetColor("_EmissionColor", Color.Lerp(colourStart, colourEnd, Mathf.PingPong(i * 2, randomTime)));
}
}
cs


Posted by 도이(doi)
,
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class textureOffset : MonoBehaviour
{
    public float verticalSpeed;
    public float horizontalSpeed;
 
    Renderer rend;
 
    void Start()
    {
        rend = GetComponent<Renderer>();
    }
 
    void Update()
    {
 
        float vOffset = Time.time * verticalSpeed;
        float hOffset = Time.time * horizontalSpeed;
 
        rend.material.mainTextureOffset = new Vector2(vOffset, hOffset);
    }
}
 
 
cs


Posted by 도이(doi)
,
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GridGenerator : MonoBehaviour
{
    public GameObject line;
    private float timeOffset;
    public int lineNum = 10;
    public float lineLength = 100.0f;
    public float speed = 1.0f;
    //public float offset = 2.0f;
    private LineRenderer[] lines;
    private LineRenderer renderLine;
    private Vector3 pos;
 
    void Start()
    {
        lines = new LineRenderer[lineNum];
        pos = this.transform.position;
 
        horizonLines();
        verticalLines();   
    }
 
    private void Update()
    {
        lineMoveToUP();
    }
 
    //first line genarte
    public void instLine()
    {
        var Obj = Instantiate(line, pos, Quaternion.identity);
        Obj.transform.SetParent(transform);
        renderLine = Obj.GetComponent<LineRenderer>();
        renderLine.useWorldSpace = false;
    }
    //vertical lines genarte
    private void verticalLines()
    {
        for (int i = 0; i < lineNum; i++)
        {
            instLine();
            for (int j = 0; j < 2; j++)
            {
                renderLine.SetPosition(j, new Vector3(pos.x + (i * (lineLength / lineNum)),
                                                      pos.y,
                                                      pos.z + (j * lineLength)));
            }
        }
    }
    //horizontal lines genarate
    private void horizonLines()
    {
        for (int i = 0; i < lineNum; i++)
        {
            instLine();
            for (int j = 0; j < 2; j++)
            {
                renderLine.SetPosition(j, new Vector3(pos.x + (j * lineLength),
                                                      pos.y,
                                                      pos.z + (i * (lineLength / lineNum))));
            }
            lines[i] = renderLine;
        }
    }
    //line move to up
    private void lineMoveToUP()
    {
        timeOffset += Time.deltaTime * speed;
        for (int i = 0; i < lineNum; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                lines[i].SetPosition(j, new Vector3(pos.x + (j * lineLength),
                                                    pos.y,
                                                    pos.z + ((i * (lineLength / lineNum) + timeOffset) % lineLength)));
            }
        }
    }
}
cs


Posted by 도이(doi)
,

radial Instantiate

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class radialInstantiate : MonoBehaviour
{
    public GameObject obj;
    public int number;
    public float radius;
    public float Rspeed;
 
    private void Awake()
    {
        for(int i=0; i<number; i++)
        {
            float angle = i * Mathf.PI * 2/number;
 
            GameObject child =
            Instantiate(obj,
            new Vector3(transform.position.x + Mathf.Cos(angle) * radius,
            transform.position.y + Mathf.Sin(angle) * radius, transform.position.z),
            Quaternion.Euler(0,0,(360/number) * i - 90));
 
            child.transform.SetParent(this.transform);
        }
    }
 
    private void Update()
    {
        //transform.rotation = Quaternion.Euler(0,0,Time.deltaTime * 100);
        transform.Rotate(00, Time.deltaTime * Rspeed);
    }
}
 
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class radialGenerator : MonoBehaviour
{
public GameObject prefab;
public int prefabNum;
public float radius;
public float animSpeed;
public bool animation = true;
private void Awake()
{
for (int i = 0; i < prefabNum; i++)
{
float angle = i * Mathf.PI * 2 / prefabNum;
//원형으로 prefab생성
GameObject child =
Instantiate(prefab, new Vector3(
transform.position.x + Mathf.Cos(angle) * radius,
transform.position.y ,
transform.position.z + Mathf.Sin(angle) * radius),
Quaternion.Euler(0, -90 -((360/prefabNum)*i), 0));
child.transform.SetParent(this.transform);
}
}
private void Update()
{
//회전 animation
if(animation)
{
transform.Rotate(0, 0, Time.deltaTime * animSpeed);
}
}
}

random Instantiate

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class randomInstantiate : MonoBehaviour
{
    public GameObject obj;
    public int number;
 
    private float time = 0;
    public float speed;
 
    private List<Transform> objs = new List<Transform>();
 
    void Awake()
    {
        for (int i = 0; i < number; i++)
        {
            float randomPosX = Random.Range(-5050);
            float randomPosY = Random.Range(7100);
 
            GameObject child =
            Instantiate(obj,
            new Vector3(transform.position.x + randomPosX, 2.5f, transform.position.z + randomPosY), Quaternion.identity);
            child.transform.SetParent(this.transform);
            objs.Add(child.transform);
        }
    }
 
    private void Update()
    {
        time += Time.deltaTime;
 
        if(time>speed)
        {
            for (int i = 0; i < number; i++)
            { 
                float randomPosX = Random.Range(-5050);
                float randomPosY = Random.Range(7100);
 
                objs[i].localPosition =
                    new Vector3(transform.position.x + randomPosX, 0, transform.position.z + randomPosY);
            }
            time = 0;
        }
 
        for (int i = 0; i < number; i++)
        {
            float randomS = Random.Range(.1f, 2f);
            objs[i].localScale = new Vector3(randomS, randomS, 1);
        }
    }
}
 
cs


'programming > c#' 카테고리의 다른 글

유니티 textureOffset  (0) 2020.02.19
유니티 line renderer로 그리드 생성  (0) 2020.02.15
유니티 Physics - 움직임의 jitter를 줄이는 방법  (0) 2020.01.04
GPU Instancing 최적화! 최적화!  (1) 2020.01.01
GetPixels  (0) 2020.01.01
Posted by 도이(doi)
,