代码地址在此Github
演示视频的地址为:演示视频
首先是实现了UserGUI的内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserGUI : MonoBehaviour {
private FirstSceneController action;
private GUIStyle fontstyle1 = new GUIStyle();
void Start () {
action = SSDirector.getInstance().currentSceneController as FirstSceneController;
fontstyle1.fontSize = 50;
}
private void OnGUI()
{
if (GUI.Button(new Rect(Screen.width / 2 - 80, Screen.height-60, 80, 60), "RESTART"))
{
action.Restart();
}
if (GUI.Button(new Rect(Screen.width / 2 + 20, Screen.height-60, 80, 60), "Pause"))
{
action.Pause();
}
if (action.flag == 0)
{
fontstyle1.normal.textColor = Color.green;
GUI.Label(new Rect(Screen.width / 2 - 180, 0, 300, 100), "Score: " +
action.score + ", Round: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
}
else if (action.flag == 1)
{
fontstyle1.normal.textColor = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 150, 0, 300, 100), "Your score is : " + action.score, fontstyle1);
}
else
{
fontstyle1.normal.textColor = Color.green;
GUI.Label(new Rect(Screen.width / 2 - 180, 0, 300, 100), "Score: " +
action.score + ", Round: " + (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), fontstyle1);
fontstyle1.normal.textColor = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 70, Screen.height/2-50, 300, 100), "Pause!", fontstyle1);
}
}
}
做出一个好的界面来,然后是导演、场记之类的还是和之前做魔鬼与传教士的差不多,这里再实现一个工厂。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkeetFactory : MonoBehaviour {
public List<GameObject> used = new List<GameObject>();
public List<GameObject> free = new List<GameObject>();
void Start () { }
public void GenSkeet()
{
GameObject skeet;
if(free.Count == 0)
{
skeet = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Skeet"), Vector3.zero, Quaternion.identity);
}
else
{
skeet = free[0];
free.RemoveAt(0);
}
float x = Random.Range(-10.0f, 10.0f);
skeet.transform.position = new Vector3(x, 0, 0);
skeet.transform.Rotate(new Vector3(x < 0? -x*9 : x*9, 0, 0));
float r = Random.Range(0f, 1f);
float g = Random.Range(0f, 1f);
float b = Random.Range(0f, 1f);
Color color = new Color(r, g, b);
skeet.transform.GetComponent<Renderer>().material.color = color;
used.Add(skeet);
}
public void RecycleSkeet(GameObject obj)
{
obj.transform.position = Vector3.zero;
free.Add(obj);
}
}
最后对于飞碟的运动使用的MoveToAction方向。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCMoveToAction : SSAction
{
public Vector3 target;
public float speed;
public static CCMoveToAction GetSSAction(Vector3 target, float speed)
{
CCMoveToAction action = CreateInstance<CCMoveToAction>();
action.target = target;
action.speed = speed;
return action;
}
public override void Start()
{
}
public override void Update()
{
if(enable)
{
this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed);
if (this.transform.position == target)
{
this.enable = false;
this.callback.SSActionEvent(this);
}
}
}
}