[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中的链表里即可。


猜你喜欢

转载自blog.csdn.net/qq_33999892/article/details/72790757