简介
这是一款基于粒子系统学习开发的小程序,使玩家通过按钮控制粒子呈现不同的效果
游戏运行效果
游戏开始运行,五种不一样颜色地粒子位于正五边形的顶点。
点击向左倾斜,给所有粒子添加一个向左的力场,粒子向左倾斜,雪花效果不会改变。
点击向右倾斜后恢复原状,再点击三次向右倾斜,使倾斜角度增大。
游戏玩法
玩家通过点击向左倾斜和向右倾斜两个按钮,控制施加在粒子上的力场,控制粒子呈现出不同的效果。
游戏代码介绍
flare : 一个 ParticleSystem 的子类,实现了左风和右风的控制,继承自 Monobehavior 类,定义了leftwind和rightwind两个控制函数,控制粒子的力场方向。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flare : MonoBehaviour
{
ParticleSystem particleSystem;
ParticleSystem.ForceOverLifetimeModule forceMode;
ParticleSystem.ColorOverLifetimeModule colorMode;
int windPower;
void Start()
{
particleSystem = GetComponent<ParticleSystem>();
forceMode = particleSystem.forceOverLifetime;
colorMode = particleSystem.colorOverLifetime;
}
public void LeftWind()
{
ParticleSystem.MinMaxCurve temp = forceMode.x;
temp.constantMax += -0.3f;
forceMode.x = temp;
}
public void RightWind()
{
ParticleSystem.MinMaxCurve temp = forceMode.x;
temp.constantMax += 0.3f;
forceMode.x = temp;
}
}
UserGUI : 一个 Monobehavior 的子类,提供 GUI 按钮用于控制多个 flare 类的实例的风向。定义五团粒子flare1-5,定义按钮位置大小和功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserGUI : MonoBehaviour
{
public flare flare1, flare2, flare3,flare4, flare5;
void Start()
{
}
void Update()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(10, 150, 60, 30), "向左倾斜"))
{
flare1.LeftWind();
flare2.LeftWind();
flare3.LeftWind();
flare4.LeftWind();
flare5.LeftWind();
}
if (GUI.Button(new Rect(Screen.width-60, 150, 60, 30), "向右倾斜"))
{
flare1.RightWind();
flare2.RightWind();
flare3.RightWind();
flare4.RightWind();
flare5.RightWind();
}
}
}