'Unity'에 해당되는 글 36건

  1. 2019.04.18 <데이터 로드, 월드 구성, by포토샵>미완성
  2. 2019.04.14 GameObject, TimedeltaTime
  3. 2019.04.12 찾아볼것 리스트
  4. 2019.04.12 <죽어라돌덩이>

<데이터 로드, 월드 구성, by포토샵>미완성

Unity/과제 2019. 4. 18. 00:34


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
namespace SG.scenes
{
    public class App : MonoBehaviour
    {
        public Camera uiCamera;
 
        private void Awake()
        {
            //로드된 직후 호출
            Object.DontDestroyOnLoad(this);
        }
 
        // Start is called before the first frame update
        void Start()
        {
            //객체 생성시 호출
            var oper = SceneManager.LoadSceneAsync("Logo");
            oper.completed += (AsyncOper) =>
              {
                  Debug.Log("로고 씬 로드 완료");
                  var logo = GameObject.FindObjectOfType<Logo>();
                  logo.Init(this.uiCamera);
              };
        }
    }
}
 
 
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
namespace SG.scenes
{
    public class Logo : MonoBehaviour
    {
        public UILogo uiLogo;
        public System.Action OnWaitForSeconds;
 
        public void Init(Camera uiCamera)
        {
            this.uiLogo.OnFadeInComplete = () =>//3완료
            {
                Debug.Log("페이드 인 완료");
 
                //타이틀 씬으로 전환
                var oper = SceneManager.LoadSceneAsync("Title");
                oper.completed += (AsyncOper) =>
                {
                    Debug.Log("로고 씬 로드 완료");
                    var title = GameObject.FindObjectOfType<Title>();
                    title.Init(uiCamera);
                };
            };
            this.uiLogo.OnFadeOutComplete = () =>//1완료
            {
                Debug.Log("페이드 아웃 완료");
 
                //3초기달
                this.OnWaitForSeconds = () =>//2완료
                {
                    StartCoroutine(this.uiLogo.FadeIn());//3
                };
                StartCoroutine(this.WaitForSeconds(2));//2
            };
 
            this.uiLogo.Init(uiCamera);
            StartCoroutine(this.uiLogo.FadeOut());//1
        }
        private IEnumerator WaitForSeconds(float sec)
        {
            yield return new WaitForSeconds(sec);
            OnWaitForSeconds();
        }
    }
}
 
 
cs

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
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
 
public class AssetManager
{
    public enum eDataType
    {
        None = -1,
        CharacterData,
        MonsterGroupData,
        StageData
    }
 
    private static AssetManager Instance;
    public Dictionary<string, GameObject> dicAssetData;
 
    private AssetManager()
    {
        this.dicAssetData = new Dictionary<string, GameObject>();
    }
 
    public static AssetManager GetInstance()
    {
        if (AssetManager.Instance == null)
        {
            AssetManager.Instance = new AssetManager();
        }
        return AssetManager.Instance;
    }
 
    public void LoadDatas<T>(string path)
    {
        var asset = Resources.Load<TextAsset>(path);
        var arrDatas = JsonConvert.DeserializeObject<T[]>(asset.text);
        foreach(var data in arrDatas)
        {
            //데이터 넣기
        }
    }
}
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
 
public class DataManager
{
    public enum eDataType
    {
        None = -1,
        CharacterData,
        MonsterGroupData,
        StageData
    }
 
    private static DataManager Instance;
    public Dictionary<int, CharacterData> dicCharacterData = new Dictionary<int, CharacterData>();
    public Dictionary<int, MonsterGroupData> dicMonsterGroupData = new Dictionary<int, MonsterGroupData>();
    public Dictionary<int, StageData> dicStageData = new Dictionary<int, StageData>();
 
    private DataManager()
    {
 
    }
 
    public static DataManager GetInstance()
    {
        if (DataManager.Instance == null)
        {
            DataManager.Instance = new DataManager();
        }
        return DataManager.Instance;
    }
 
    public void LoadDatas<T>(string path, eDataType dataType)
    {
        var asset = Resources.Load<TextAsset>(path);
        var arrDatas = JsonConvert.DeserializeObject<T[]>(asset.text);
        foreach(var data in arrDatas)
        {
            //데이터 넣기
        }
    }
}
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
namespace SG.scenes
{
    public class Title : MonoBehaviour
    {
        public UITitle uiTitle;
        public Camera uiCamera;
        public bool isRead;
 
        private void Awake()
        {
            this.uiTitle.btn.onClick.AddListener(() => 
            {
                if (!isRead)
                {
                    Debug.Log("아직 실행이 준비되지 않았습니다.");
                }
                else
                {
                    //인게임 실행
                    var oper = SceneManager.LoadSceneAsync("InGame");
                    oper.completed += (AsyncOper) =>
                    {
                        Debug.Log("인게임씬 로드 완료");
                        var inGame = GameObject.FindObjectOfType<InGame>();
                        inGame.Init(this.uiCamera);
                    };
                }
            });
 
        }
 
        public void Init(Camera uiCamera)
        {
            this.uiCamera = uiCamera;
            this.uiTitle.Init(uiCamera);
            this.uiTitle.OnFadeOutComplete = () =>
            {
                Debug.Log("똬이럴실행");
                isRead = true;
            };
            StartCoroutine(this.uiTitle.FadeOut());
        }
    }
}
 
cs

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
namespace SG.scenes
{
    public class InGame : MonoBehaviour
    {
        public Canvas canvas;
        public Image dim;
        public GameObject heroGo;
        public GameObject monsterGo;
        public System.Action OnFadeOutComplete;
        public System.Action OnFadeInComplete;
 
        private void Awake()
        {
            DataManager.GetInstance().LoadDatas<Character>("Data/character_data"0);
            DataManager.GetInstance().LoadDatas<MonsterGroupData>("Data/monster_group_data"0);
            DataManager.GetInstance().LoadDatas<StageData>("Data/stage_data"0);
        }
 
        public void Init(Camera uiCamera)
        {
            this.canvas.worldCamera = uiCamera;
            Debug.Log("와 게임 시작이다!");
            var hero = this.heroGo.GetComponent<Character>();
            var monster = this.monsterGo.GetComponent<Character>();
            //System.Action OnFadeOutComplete = () =>
            //  {
            //      System.Action OnFadeInComplete = () =>
            //      {
 
            //      };
            //      this.FadeIn();
            //  };
            //this.Fadeout();
        }
 
        private void PreLoad()
        {
            var arrObject=  Resources.LoadAll("PreFabs");
            foreach(var obj in arrObject)
            {
                //데이터 넣기
            }
        }
 
        public void Fadeout()
        {
            StartCoroutine(FadeOutImpl());
        }
 
        public IEnumerator FadeOutImpl()
        {
            var color = this.dim.color;
            float alpha = color.a;
 
            while (true)
            {
                alpha -= 0.016f;
                color.a = alpha;
                this.dim.color = color;
 
                if (alpha <= 0)
                {
                    alpha = 0;
                    break;
                }
                yield return null;
            }
            this.OnFadeOutComplete();
        }
 
        public void FadeIn()
        {
            StartCoroutine(FadeInImpl());
        }
        public IEnumerator FadeInImpl()
        {
            var color = this.dim.color;
            float alpha = color.a;
 
            while (true)
            {
                alpha += 0.016f;
                color.a = alpha;
                this.dim.color = color;
 
                if (alpha >= 1)
                {
                    alpha = 1;
                    break;
                }
                yield return null;
            }
            this.OnFadeInComplete();
        }
    }
}
 
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class StageData
{
    public int id;
    public string name;
    public string prefab_name;
    public int monster_group_id;
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MonsterGroupData
{
    public int id;
    public int group_id;
    public int monster_id;
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
namespace SG.scenes
{
    public class CharacterData
    {
        public int id;
        public string name;
        public string prefab_name;
        public string thumb_name;
        public string thumb_hex_color;
        public string idle_anim_name;
        public string run_anim_name;
        public string attack_anim_name;
        public string hit_anim_name;
        public string death_anim_name;
    }
}
 
 
 
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class TestAttackAnim : MonoBehaviour
{
    public GameObject bullGo;
    public Button btn1;
    public Button btn2;
    public Button btn3;
    private Animator anim;
 
    // Start is called before the first frame update
    private void Awake()
    {
        this.anim = this.bullGo.GetComponent<Animator>();
 
        this.btn1.onClick.AddListener(() =>
        {
            this.anim.SetBool("idle"true);
        });
        this.btn2.onClick.AddListener(() =>
        {
            this.anim.SetBool("run"true);
        });
        this.btn3.onClick.AddListener(() =>
        {
            this.anim.SetBool("attack_01"true);
            StartCoroutine(this.WaitforAttacComplete(() =>
            {
                Debug.Log("타격!");
            }, () =>
            {
                Debug.Log("공격끝");
            }));
        });
    }
    private IEnumerator WaitforAttacComplete(System.Action onAttack, System.Action onAttackComplete)
    {
        yield return new WaitForSeconds(0.75f);
        onAttack();
        yield return new WaitForSeconds(0.25f);
        onAttackComplete();
    }
}
cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UITitle : MonoBehaviour
{
    public Canvas canvas;
    public Image dim;
    public System.Action OnFadeOutComplete;
    public System.Action OnFadeInComplete;
    public Button btn;
 
    public void Init(Camera uiCamera)
    {
        this.canvas.worldCamera = uiCamera;
    }
 
    public IEnumerator FadeOut()
    {
        var color = this.dim.color;
        float alpha = color.a;
 
        while (true)
        {
            alpha -= 0.016f;
            color.a = alpha;
            this.dim.color = color;
 
            if (alpha <= 0)
            {
                alpha = 0;
                break;
            }
            yield return null;
        }
        this.OnFadeOutComplete();
    }
 
    public IEnumerator FadeIn()
    {
        var color = this.dim.color;
        float alpha = color.a;
 
        while (true)
        {
            alpha += 0.016f;
            color.a = alpha;
            this.dim.color = color;
 
            if (alpha >= 1)
            {
                alpha = 1;
                break;
            }
            yield return null;
        }
        this.OnFadeInComplete();
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    private Animation anim;
 
    private void Awake()
    {
        this.anim = this.gameObject.GetComponent<Animation>();
    }
 
    public void Init(Vector3 initPosition)
    {
        this.gameObject.transform.position = initPosition;
    }
}
 
cs


'Unity > 과제' 카테고리의 다른 글

<설계구조를 다시 짜보았다.>  (0) 2019.04.21
<캐릭터 생성 월드로>한단계 진화함  (0) 2019.04.19
:

GameObject, TimedeltaTime

Unity/찾아본것 2019. 4. 14. 22:37

게임 오브젝트

└컴포넌트

└트랜스폼

└프리팹

└정적 게임오브젝트

└런타임시 프리팹의 인스턴스화

└작업저장


게임오브젝트

게임오브젝트는 유니티에디터에서 가장 중요한 개념이다.

게임에 존재하는 모든 오브젝트가 게임 오브젝트이다.

게임오브젝트 자체로는 아무것도 할 수 없고, 게임 오브젝트가 캐릭터,환경,특수효과가 되기 위해선 ?프로퍼티를 부여해야한다.

?프로퍼티 : 값


컴포넌트

게임 오브젝트가 포함하는 프로퍼티(값)들

기본적으로 모든 오브젝트는 Transform(좌표위치Position, 회전각도Rotation, 크기Scale)을 가지고 있다..


정적 게임 오브젝트(Static GameObject)

게임의 최적화를 위해서 게임플레이중 움직이지 않는 오브젝트는 정적 오브젝트로하여

정적오브젝트 들을 '배치(arrangement)'라는 큰 오브젝트로 결합시켜 렌더링을 최적화 할 수 있다.

게임오브젝트 인스펙터 맨 오른쪽 상단에는 정적 체크박스 및 메뉴가 있다.


프리팹

오브젝트를 복사하기만 하면, 복제본이 생성되지만 개별편집 할 수 있습니다.

일반적으로 모든 인스턴스가 동일한 프로퍼티를 가지는 것이 좋다.


프리팹 에셋타입으로 오브젝트를 저장할 수 있다

프리팹은 씬에서 새로운 오브젝트인스턴스를 생성할 수 있는 템플릿으로 사용됩니다.

인스턴스의 각 컴포넌트와 설정을 개별적으로 '오버라이드' 할 수도 있음.


Time.deltaTime

public static float deltaTime;

지난프레임이 완료되는 데 까지 걸린 시간을 나타낸다. 단위는초를 사용한다(읽기전용)


사용자의 frame rate를 독립적으로 적용하기 위해 사용함.


매 프레임마다 어떤값을 더하거나 빼는 계산을 하는 경우에 Time.deltaTime을 곱해서 사용한다.

ex)오브젝트를 프레임당 10미터가 아닌 초당 10미터를 움직이고 싶은 경우


?MonoBehaviour의 ?FixedUpdate에서 호출되는 경우에 고정 프레임률 deltaTime을 반환한다.


?MonoBehaviour

모든 스크립트가 상속받는 기본 클래스


?MonoBehaviour.FixedUpdate()

MonoBehaviour이 활성화 되어 있는 경우에, 매 고정된 프레임율의 프레임 마다 호출됩니다.

Time.deltaTime을 사용하여, 지난 Update호출로부터 경과된 시간을 받아옵니다.

:

찾아볼것 리스트

Unity/찾아볼것 2019. 4. 12. 20:28

프리팹




게임오브젝트 검색하기!

Object.FindObjectOfType

https://tenlie10.tistory.com/90

'Unity > 찾아볼것' 카테고리의 다른 글

unity editor select list  (0) 2019.08.21
custom Inspector 겹치기 오류  (0) 2019.07.23
<uBMSC> 리듬게임  (0) 2019.05.09
Unity Editor확장입문 참고  (0) 2019.05.07
:

<죽어라돌덩이>

Unity/수업내용 2019. 4. 12. 01:40




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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App : MonoBehaviour
{
    public GameObject heroGo;
    public GameObject monsterGo;
    
    // Start is called before the first frame update
    void Start()
    {
        var hero = this.heroGo.GetComponent<Character>();
        var monster = this.monsterGo.GetComponent<Character>();
 
        hero.Init(new Vector3(101));
        monster.Init(new Vector3(505));
 
 
        hero.Move(monster);
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
cs


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    private float speed = 1f;
    private float lange = 0.7f;
    private int hp = 10;
    private Vector3 targetPosition;
    private Vector3 attackTargetPosition;
    private float moveDis;
 
    private Character target;
 
    private bool isMoveComplete = false;
    private bool isMoveStart = false;
 
    private bool isAttackStart = false;
 
    private Animation anim;
 
    private float timer = 0.0f;
    private float waitingTime = 1.5f;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.gameObject.GetComponent<Animation>();
    }
 
    public void Init(Vector3 initPosition)
    {
        this.gameObject.transform.position = initPosition;
    }
 
    public void Move(Character target)
    {
        this.target = target;
        this.targetPosition = target.gameObject.GetComponent<Character>().transform.position;
        this.moveDis = Vector3.Distance(this.targetPosition, this.gameObject.GetComponent<Character>().transform.position);
        this.isMoveStart = true;
    }
 
    public void MoveStart()
    {
        anim.Play("run@loop");
        var normVec = (this.targetPosition - this.gameObject.GetComponent<Character>().transform.position).normalized;
 
        if (isMoveComplete == false)
        {
            if (moveDis > this.lange)
            {
                //계속앞으로 이동
                moveDis -= speed * Time.deltaTime;
                this.gameObject.GetComponent<Character>().transform.position += this.speed * normVec * Time.deltaTime;
            }
            else
            {
                isMoveStart = false;
                isMoveComplete = true;
                isAttackStart = true;
                anim.Play("idle@loop");
            }
        }
    }
 
    public void AttackStart()
    {
        anim.Play("attack_sword_01");
        var normVec = (this.targetPosition - this.gameObject.GetComponent<Character>().transform.position).normalized;
        
        this.target.gameObject.GetComponent<Animation>().Play("Anim_Damage");
        this.hp--;
        
        Debug.Log(this.hp);
 
        if (this.hp <= -1)
        {
            Debug.Log("으앙쥬금");
            anim.Play("idle@loop");
            this.isAttackStart = false;
        }
    }
 
    // Update is called once per frame
    void Update()
    {
        if(this.hp<=-1)
        {
            this.target.gameObject.GetComponent<Animation>().Play("Anim_Death");
            this.hp = 0;
        }
        if (isMoveStart == true)
        {
            this.MoveStart();
        }
        this.timer += Time.deltaTime;
        if(this.timer>waitingTime)
        {
            if (isAttackStart == true)
            {
                this.AttackStart();
            }
            this.timer = 0;
        }
    }
}
 
cs


//고개돌리기, 타이밍싱크, 공격방식 바꾸면서 때리기

'Unity > 수업내용' 카테고리의 다른 글

(1)BGLoop  (0) 2019.05.01
LayerMask  (0) 2019.05.01
자습서1(WASD이동, 마우스 포인트 Look, 라인렌더링  (0) 2019.04.30
<캐릭터 조작>화면 터치, WASD, 조이스틱  (0) 2019.04.25
HUDTest  (0) 2019.04.25
: