'코드에서 애니메이션 바꾸기'에 해당되는 글 1건

  1. 2019.12.09 12.09 충돌감지와 sprite활용하기(delegate)

12.09 충돌감지와 sprite활용하기(delegate)

C#/실습 2019. 12. 9. 17:25

코드:

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
public class App : MonoBehaviour
{
    public Button btnShoot;
    public Transform bulletInitPoint;
    private GameObject bulletPrefab;
    private Sprite explosionsprite;
    
    // Start is called before the first frame update
    void Start()
    {
        this.LoadResources();
        this.btnShoot.onClick.AddListener(() =>
        {
            Debug.Log("발사!");
            this.Shoot();
 
        });
    }
    private void LoadResources()
    {
        this.bulletPrefab = Resources.Load<GameObject>("Bullet");
        this.explosionsprite = Resources.Load<Sprite>("explosion3");
 
    }
    private void Shoot()
    {
        GameObject bulletModel = Instantiate(bulletPrefab);
        Bullet bullet = bulletModel.AddComponent<Bullet>();
        bulletModel.transform.position = this.bulletInitPoint.position;
        bullet.ShootCoroutine = bullet.ShootImple();
        StartCoroutine(bullet.ShootCoroutine);
        bullet.del = () =>
        {
            StopCoroutine(bullet.ShootCoroutine);
            GameObject explosionGO = new GameObject("explosion");
            explosionGO.transform.position = bullet.transform.position;
            SpriteRenderer spriteRenderer = explosionGO.AddComponent<SpriteRenderer>();
            spriteRenderer.sprite = Instantiate(explosionsprite);
            Animator animator = explosionGO.AddComponent<Animator>();
            animator.runtimeAnimatorController = 
            (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(
            Resources.Load("ExplosionCon"typeof(RuntimeAnimatorController)));
            explosion explosion = explosionGO.AddComponent<explosion>();            
            Destroy(bullet.gameObject);
 
        };
        
        
        
        
    }
    
    
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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;
 
public class explosion : MonoBehaviour
{
    public void whenExplode()
    {
        Destroy(this.transform.root.gameObject);
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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 System;
public class Bullet : MonoBehaviour
{
    public Action del;
    public IEnumerator ShootCoroutine;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    //private void OnCollisionEnter(Collision collision)
    //{                
    //    App.del();        
    //}
    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "wall")
        {
            this.del();
        }
        
    }
    public IEnumerator ShootImple()
    {
        while (true)
        {
            var vc = this.transform.up;
            this.transform.position += vc * 1 * Time.deltaTime;
 
            yield return null;
        }
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

참조:

Sprite 파일

https://www.pngguru.com/free-transparent-background-png-clipart-zceay

 

Sprite GameMaker: Studio Animation 2D computer graphics, explosion sprite transparent background PNG clipart | PNGGuru

PNG Clipart Information Dimensions 960x384px File size 113.87KB MIME type Image/png dominant color orange Resize PNG Clipart online resize by width resize by height

www.pngguru.com

스프라이트 애니메이션

https://m.blog.naver.com/PostView.nhn?blogId=heartybrain1&logNo=221142037112&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

유니티 - 스프라이트 시트(sprite sheet) 사용하기, 스프라이트 애니메이션

유니티에서 sprite sheet 사용하기, sprite 애니메이션 1)스프라이트 시트(sprite sheet)는 2D 애니메이션...

blog.naver.com

코드에서 애니메이션 바꾸기

https://playground10.tistory.com/95

 

[유니티] 코드에서 애니메이션 바꾸기

프리팹은 하나이고 애니메이션만 계속 코드에서 동적으로 바꾸고 싶을 때 아래와 같이 해주면 됩니다. 1. 먼저 해당 프리팹에 animator 컴포넌트를 추가해줍니다. (기존에 프리팹을 Player라고 만들었고 스크립트..

playground10.tistory.com

 

: