赞
踩
此学习内容根据up主制作
在创建好2D工程文件打开后,可以直接双击运行资源包运行在编辑器打开。
添加背景图片并将摄像机的视角大小设置为300
全选所有图片拖拽进场景unity会帮我们创建一个带有由这些图片组成的序列帧动画的物体。
这是由于图层渲染顺序的问题
解决方法
将Order in layer默认0设置为1 ,大于背景图层即可
我们可以通过调整sorting layer
这时候我们便可以通过默认预设来解决,每一个组件都会默认应用我们的预设
看成功了
1.首先我们创建太阳花发光与不发光两个动画,还是直接拖拽进场景自动创建动画即可。
2.配置好太阳花的动画的切换
3.在代码逻辑上实现动画切换和太阳生成
总体代码一览
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Mathematics;
- using UnityEngine;
-
- public class SunFlower : MonoBehaviour
- {
- private Animator animator;
-
- [SerializeField]
- private float generateSunIntervalTime = 5f;
- private float countGenerateSunIntervalTime;
-
-
-
-
- [SerializeField]
- private string parameter;
-
- [SerializeField]
- private GameObject SunPrefab;
- // Start is called before the first frame update
- void Start()
- {
-
- animator = GetComponent<Animator>();
- parameter = EnumType.EnumToString(EnumType.AnimatorParameters.IsReady);
-
- countGenerateSunIntervalTime = generateSunIntervalTime;
- }
-
- // Update is called once per frame
- void Update()
- {
- if (animator.GetBool(parameter)== false)
- {
- countGenerateSunIntervalTime -= Time.deltaTime;
- if(countGenerateSunIntervalTime <= 0)
- {
- animator.SetBool(parameter, true);
- countGenerateSunIntervalTime = generateSunIntervalTime;
- }
- }
- }
- public void GenerateSun()
- {
-
- animator.SetBool(parameter, false);
- Instantiate(SunPrefab,transform.position + (Vector3)UnityEngine.Random.insideUnitCircle*50, Quaternion.identity);
-
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PeaBullet : MonoBehaviour
- {
- [SerializeField]
- private float speed = 1f;
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.position += Vector3.right*speed;
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Peashooter : MonoBehaviour
- {
- [SerializeField]
- private GameObject Bullet;
- [SerializeField]
- private float attackIntevalTime = 5f;
- private float countTime = 0;
- [SerializeField]
- private Vector3 bulletPosition;
-
- // Start is called before the first frame update
- void Start()
- {
- countTime = attackIntevalTime;
- }
-
- // Update is called once per frame
- void Update()
- {
- if (countTime > 0)
- {
- countTime -= Time.deltaTime;
- }
- else
- {
- Instantiate(Bullet, bulletPosition, Quaternion.identity, transform);
- countTime = attackIntevalTime;
- }
-
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Sun : MonoBehaviour
- {
- [SerializeField]
- private float destroyTime = 5f;
- private float countDestroyTime;
-
-
- // Start is called before the first frame update
- void Start()
- {
- countDestroyTime = destroyTime;
- }
-
- // Update is called once per frame
- void Update()
- {
- countDestroyTime -= Time.deltaTime;
- if (countDestroyTime <= 0)
- {
- Destroy(gameObject);
- }
- }
- }

对应类名挂载到对应物体上。
效果一览
这是我第一次写文章,发现有很多的问题,当然代码写得也不是很好,有很多预期效果没有实现好。谢谢你的阅读
在后面的笔记中我们详细记录我遇到学习问题,不会过多的记录,毕竟视频做得已经很好了,可以查漏补缺。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。