[Unity]製作一個彈幕系統

1.大致思路?

利用OnGUI,顯示一系列會動的字,這一系列的彈幕可以用一個隊列維護,每一幀都更新隊列中彈幕的位置,並做一次檢測,如果隊列中彈幕位置已經不在屏幕內,可將該彈幕移除隊列。

2.彈幕可以有的屬性?

1.速度(矢量)

2.位置

3.顏色

4.字體

5.大小

6.材質

7.旋轉

3.代碼編寫

Pluck類:

public class Pluck
{
	Rect MyRect;
	Vector2 velocity;
	Vector2 position;
	string contain;
	Color c;
	int size;
	int font;
	public static Font f1;
	public static Font f2;
	public static Font f3;
	public static Font f4;
	public Pluck(int Size,int InitPos,float _scale,string _contain,Color col,Vector2 velocity,int f)
	{
		this.velocity=velocity;
		Vector2 Init_pos;
		switch(InitPos)
		{
		case (int)PluckInitPosition.Bottom:
			Init_pos=new Vector2(Mathf.Lerp(10,Screen.width-10,_scale),10);
			break;
		case (int)PluckInitPosition.Top:
			Init_pos=new Vector2(Mathf.Lerp(10,Screen.width-10,_scale),Screen.height-10);
			break;
		case (int)PluckInitPosition.Left:
			Init_pos=new Vector2(10,Mathf.Lerp(10,Screen.height-10,_scale));
			break;
		case (int)PluckInitPosition.Right:
			Init_pos=new Vector2(Screen.width-10,Mathf.Lerp(10,Screen.height-10,_scale));
			break;
		default:
			Init_pos=new Vector2();
			break;
		}
		position=Init_pos;
		MyRect=new Rect(Init_pos,new Vector2(150,150));
		size=Size;
		contain=_contain;
		this.velocity=velocity;
		c=col;
		this.font=f;
	}
	public bool IsNotRender()
	{
		if(Vector2.SqrMagnitude(MyRect.center-position)>1.0f)
		{
			if(OutOfMaxRect(MyRect))
			{
				return true;
			}
		}
		return false;
	}
	bool OutOfMaxRect(Rect r)
	{
		if (r.yMin > Screen.height || r.yMax < 0 || r.xMax < 0 || r.xMin > Screen.width) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public void updateposition()
	{
		MyRect.center += velocity;
	}
	public void Render()
	{
		GUIStyle gs = new GUIStyle ();
		switch(font)
		{
		case (int)PluckFont.default_font:
			gs.font = f1;
			break;
		case (int)PluckFont.font_one:
			gs.font = f2;
			break;
		case (int)PluckFont.font_two:
			gs.font = f3;
			break;
		case (int)PluckFont.font_three:
			gs.font = f4;
			break;
		default:
			gs.font = f1;
			break;
		}
		gs.fontSize =size;

		GUI.backgroundColor=Color.clear;
		gs.normal.textColor = c;
		GUI.TextField(MyRect,contain,gs);
	}
}
PluckManager類:

public class PluckManager : MonoBehaviour 
{

	[HideInInspector]
	public List<Pluck> pluckqueue=new List<Pluck>();
	//float timer;
	//public Material mat_std;
	//public GameObject TextPrefab;
	public  Font f1;
	public  Font f2;
	public  Font f3;
	public  Font f4;
	void Start()
	{
		Pluck.f1 = f1;
		Pluck.f2 = f2;
		Pluck.f3 = f3;
		Pluck.f4 = f4;
		//pluckqueue = new List<Pluck> ();
	}
	void OnGUI()
	{
		foreach(Pluck pl in pluckqueue)
		{
			pl.Render ();
		}
	}
	void Update()
	{
		List<Pluck> BufferPluck=new List<Pluck>();
		foreach(Pluck pl in pluckqueue)
		{
			if (pl.IsNotRender ()) 
			{
				BufferPluck.Add (pl);
			}
		}
		foreach(Pluck pl in BufferPluck)
		{
			pluckqueue.Remove (pl);
		}
		BufferPluck.Clear ();
		foreach(Pluck pl in pluckqueue)
		{
			pl.updateposition ();
		}
	}
}
代碼的調用,用戶以任意形式輸入話的時候,new一個pluck,將其插入pluckmanager中的鏈表裏即可。


發佈了27 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章