관리 메뉴

caLAB

유니티 linearGenerator 본문

Unity/유니티 개발

유니티 linearGenerator

도이(doi) 2020. 9. 21. 15:56
728x90

유니티에서 정렬 및 생성하는 코드 

mirror 기능도 있음.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class linearGenerator : MonoBehaviour
{
    public Transform prefab;
    public int numPrefab = 10;
    public float step = 2.0f;
    public float gapBetween = 10.0f;

    public bool horizontal = false;
    public bool vertical = false;
    public bool mirror = false;

    void Start()
    {
        //수평으로 prefab 생성
        if (horizontal && !mirror)
        {
            for (int i = 0; i < numPrefab; i++)
            {
                Transform pre = Instantiate(prefab, new Vector3(i * step + transform.position.x, 
                                                                transform.position.y, 
                                                                transform.position.z), 
                                                        Quaternion.identity);
                pre.transform.SetParent(transform);
            }
        }
        //수직으로 prefab 생성
        if (vertical & !mirror)
        {
            for (int i = 0; i < numPrefab; i++)
            {
                Transform pre = Instantiate(prefab, new Vector3(transform.position.x, 
                                                                transform.position.y, 
                                                                i * step + transform.position.z), 
                                                        Quaternion.identity);
                pre.transform.SetParent(transform);
            }
        }
        //거울모드
        if(mirror)
        {
            if (horizontal)
            {
                for (int i = 0; i < numPrefab; i++)
                {
                    Transform pre = Instantiate(prefab, 
                                                new Vector3(i * step + transform.position.x, 
                                                            transform.position.y, 
                                                            transform.position.z - gapBetween/2), 
                                                    Quaternion.identity);
                    Transform mPre = Instantiate(prefab,
                                                new Vector3(i * step + transform.position.x, 
                                                            transform.position.y, 
                                                            transform.position.z + gapBetween / 2),
                                                    Quaternion.identity);
                    pre.transform.SetParent(transform);
                    mPre.transform.SetParent(transform);
                }
            }
            if (vertical)
            {
                for (int i = 0; i < numPrefab; i++)
                {
                    Transform pre = Instantiate(prefab, 
                                                new Vector3(transform.position.x - gapBetween/2, 
                                                            transform.position.y, 
                                                            i * step + transform.position.z), 
                                                    Quaternion.identity);
                    Transform mPre = Instantiate(prefab,
                                                new Vector3(transform.position.x + gapBetween/2, 
                                                            transform.position.y, 
                                                            i * step + transform.position.z),
                                                    Quaternion.identity);
                    pre.transform.SetParent(transform);
                    mPre.transform.SetParent(transform);
                }
            }
        }
        else
            return;
    }
}
728x90
반응형
Comments