728x90

์—ฐ๊ฒฐ๋œ TIL

 

Unity Editor๋ผ๋Š”  ๋„ค์ž„์ŠคํŽ˜์ด์Šค(ํด๋ž˜์Šค์˜ ์ง‘ํ•ฉ)์˜ ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์œ ๋‹ˆํ‹ฐ ์—๋””ํ„ฐ๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›ํ•˜๋Š” ๋งคํฌ๋กœ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฉ”๋‰ด ์ฐฝ์— ์ถ”๊ฐ€ํ•œ๋‹ค๋“ ๊ฐ€..!

 

๊ฐœ๋ฐœํˆด์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•ด๋ณธ ๊ฑด ์ฒ˜์Œ์ด๋ผ ์‹ ๊ธฐํ•ด์„œ ์ •๋ฆฌํ•ด๋ด„

 

๊ธฐ๋Šฅ์„ ๋ณด๋ ค๊ณ , ์‹ค์ œ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ ์ค‘ ์ผ๋ถ€๋ฅผ ๋ณ€ํ˜•ํ•ด์„œ ๊ฐ€์ ธ์˜จ ๊ฑฐ๋ผ

๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ๋ช… ๋“ฑ์€ ์ฝ”๋“œ ๊ฐ„ ํ˜ธํ™˜์ด ์•ˆ๋ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ ๋งŒ๋“  ์ปค์Šคํ…€ ๋ฉ”์†Œ๋“œ ์„ค๋ช…์€ ์ƒ๋žตํ•œ๋‹ค.


1. Assets/Editor์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.

๋งคํฌ๋กœ๊ฐ€ ๋  ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ

2. Unity Editor ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; // ์ถ”๊ฐ€ํ•จ

// EditorWindow ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์—๋””ํ„ฐ ์œˆ๋„์— ๋ฉ”๋‰ด ์ถ”๊ฐ€
public class MyWindow : EditorWindow
{

    //***** Add Window Menu
    [MenuItem("MyWindow/AddComponent")]
    static void init()
    {

        // AddComponent ๋ฉ”๋‰ด๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ƒˆ ์ฐฝ์ด ๋œฌ๋‹ค.
        MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
        window.Show();
    }

    //***** Window Design
    private void OnGUI()
    {
        if (GUILayout.Button("add component"))

            // Selection ํด๋ž˜์Šค์˜ activeTransform์„ ์‚ฌ์šฉํ•˜๋ฉด
            // ํ˜„์žฌ ์„ ํƒ๋œ ์˜ค๋ธŒ์ ํŠธ์˜ transform์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
            // ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  .gameObject ์ถ”๊ฐ€ํ•จ
            UserCustomMethod(Selection.activeTransform.gameObject);
    }

3. ( ๋ฉ”์†Œ๋“œ: ์‚ฌ์šฉํ•œ API๋งŒ ์ฐธ๊ณ   )

// ....

  public GameObject targetObject;
  public GameObject parentObject;
  int finMeshCount = 0;

    // Function
    void UserCustomMethod(GameObject gameObject)
    {
        for(int i=0; i<gameObject.transform.childCount; i++)
        {
            // ์˜ค๋ธŒ์ ํŠธ์˜ child์— ์ ‘๊ทผํ•˜๋ ค๋ฉด transform์—์„œ Getchild ์‚ฌ์šฉํ•ด์•ผ ํ•จ
            GameObject childObject = gameObject.transform.GetChild(i).gameObject;
            AddForStructureParent(childObject);
            AddForStructureChild(childObject);
            parentObject = childObject;

            // GameObject.GetComponent<Type>()์œผ๋กœ ์˜ค๋ธŒ์ ํŠธ์˜ ์ปดํฌ๋„ŒํŠธ ์–ป์„ ์ˆ˜ ์žˆ์Œ
            // .์†์„ฑ๋ช… ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ์†์„ฑ๋„ get, setํ•  ์ˆ˜ ์žˆ์Œ
            childObject.GetComponent<CustomScript>().parentObj = parentObject.GetComponent<CustomScript>();
        }
    }

  
    void UserCustomMethod3(GameObject gameObject)
    {
        if (gameObject.GetComponent<MeshCollider>() == null)

            // ์˜ค๋ธŒ์ ํŠธ์— ํƒ€์ž…์„ ์ง€์ •ํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
            gameObject.AddComponent<MeshCollider>();

        // ์Šคํฌ๋ฆฝํŠธ๋ช…์„ <ํƒ€์ž…>์— ๋„ฃ์–ด์ฃผ๋ฉด ์Šคํฌ๋ฆฝํŠธ ์ปดํฌ๋„ŒํŠธ๋„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
        if (gameObject.GetComponent<MyScript>() == null)
            gameObject.AddComponent<MyScript>();
    }

 

 

 

728x90

+ Recent posts