Unity3d 如何在unity中通过脚本(我想将我的标签和按钮添加到面板)制作带有浅白色边框的面板
我想在unity中通过脚本制作一个面板,并将我的标签和按钮添加到面板中 我现有的Gui模式弹出代码如下所示Unity3d 如何在unity中通过脚本(我想将我的标签和按钮添加到面板)制作带有浅白色边框的面板,unity3d,Unity3d,我想在unity中通过脚本制作一个面板,并将我的标签和按钮添加到面板中 我现有的Gui模式弹出代码如下所示 using UnityEngine; using System; using System.Collections.Generic; using System.Collections.ObjectModel; // example: // HFTDialog.MessageBox("error", "Sorry but you're S.
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// example:
// HFTDialog.MessageBox("error", "Sorry but you're S.O.L", () => { Application.Quit() });
public class HFTDialog : MonoBehaviour {
Rect m_windowRect;
Action m_action;
string m_title;
string m_msg;
string[] m_myNewsTitleColl;
string[] m_myColl;
GUIStyle labelStyle, labelStyleShadow, buttonStyle, sliderStyle, sliderThumbStyle;
// The position on of the scrolling viewport
public Vector2 scrollPosition = Vector2.zero;
private GUIStyle guiStyle; //create a new variable
static public void MessageBox(string[] myNewsTitleColl, string[] myColl,string title, string msg, Action action)
{
GameObject go = new GameObject("HFTDialog");
HFTDialog dlg = go.AddComponent<HFTDialog>();
dlg.Init(myNewsTitleColl,myColl,title, msg, action);
}
static public void CloseMessageBox(GameObject go)
{
Destroy(go.gameObject);
}
void Init( string[] myNewsTitleColl,string[] myColl,string title, string msg, Action action)
{
m_title = title;
m_msg = msg;
m_action = action;
m_myColl=myColl;
m_myNewsTitleColl=myNewsTitleColl;
}
void OnGUI()
{
const int maxWidth = 640;
const int maxHeight = 480;//480;
int width = Mathf.Min(maxWidth, Screen.width - 20);
int height = Mathf.Min(maxHeight, Screen.height - 20);
m_windowRect = new Rect((Screen.width - width) / 2,(Screen.height - height) / 2,width,height);
GUI.color = Color.white;
GUI.backgroundColor = Color.yellow;
m_windowRect = GUI.Window(0, m_windowRect, WindowFunc, m_title);
}
void WindowFunc(int windowID)
{
const int border = 10;
const int width = 50;
const int height = 25;
const int spacing = 10;
Rect ls = new Rect(
border,
border + spacing,
m_windowRect.width - border * 2,
m_windowRect.height - border * 2 - height - spacing);
//Start Scrollview test
scrollPosition = GUI.BeginScrollView(ls, scrollPosition, new Rect(0, 0, 600, 4800));//Increase the height if more scrolling is needed
guiStyle = new GUIStyle();
guiStyle.fontStyle = FontStyle.Bold;
guiStyle.normal.textColor = Color.white;
int myNewsTitlePositionY=border + spacing;//Initial position
if(m_myNewsTitleColl.Length==0){
Rect l = new Rect(border, myNewsTitlePositionY,m_windowRect.width - border * 2,m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, "Currently No News Available ,Please try later..",guiStyle);//This is the news title label
}
else{
for(var i = 0; i < m_myNewsTitleColl.Length; i++)
{
var NewsTitle = m_myNewsTitleColl[i];
Rect l = new Rect(border, myNewsTitlePositionY,m_windowRect.width - border * 2,m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, NewsTitle,guiStyle);//This is the news title label
myNewsTitlePositionY=myNewsTitlePositionY+33;//next y position of the news title label
}
}
int myY=-15;
for(var i = 0; i < m_myColl.Length; i++)
{
var newsItemLink = m_myColl[i];
// work with news link item here to make a button
myY=myY+33; //Y position of button
//Rect btn = new Rect(550,myY, width+25, height);
Rect btn = new Rect(510,myY, width+25, height); //when scroll enable -
if (GUI.Button(btn, "Open Link"))
{
Application.OpenURL(newsItemLink );//open browser pointing the url
}
Debug.Log ("from string array value is :"+newsItemLink );
}
//End Scrollview
GUI.EndScrollView();
//Close button
Rect b = new Rect(
m_windowRect.width - width - border,
m_windowRect.height - height - border,
width,
height);
if (GUI.Button(b, "close"))
{
Destroy(this.gameObject);
// m_action();
}
}
}
使用UnityEngine;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
//例如:
//HFTDialog.MessageBox(“错误”,“对不起,您是S.O.L”,()=>{Application.Quit()});
公共类HFTDialog:单一行为{
矩形m_windowRect;
行动m_行动;
字符串m_标题;
字符串m_msg;
字符串[]m_myNewsTitleColl;
字符串[]m_myColl;
GUI样式标签样式,标签样式阴影,按钮样式,滑块样式,滑块样式;
//滚动视口上的位置
公共向量2滚动位置=向量2.0;
private GUIStyle GUIStyle;//创建一个新变量
静态公共void消息框(字符串[]myNewsTitleColl,字符串[]myColl,字符串标题,字符串消息,操作)
{
GameObject go=新游戏对象(“HFTDialog”);
HFTDialog dlg=go.AddComponent();
dlg.Init(myNewsTitleColl、myColl、title、msg、action);
}
静态公共void CloseMessageBox(游戏对象go)
{
销毁(go.gameObject);
}
void Init(字符串[]myNewsTitleColl,字符串[]myColl,字符串标题,字符串消息,操作)
{
m_title=标题;
m_msg=msg;
m_动作=动作;
m_myColl=myColl;
m_myNewsTitleColl=myNewsTitleColl;
}
void OnGUI()
{
const int maxWidth=640;
const int maxHeight=480;//480;
int width=Mathf.Min(maxWidth,Screen.width-20);
int height=Mathf.Min(maxHeight,Screen.height-20);
m_windowRect=新的Rect((Screen.width-width)/2,(Screen.height-height)/2,width,height);
GUI.color=color.white;
GUI.backgroundColor=Color.yellow;
m_windowRect=GUI.Window(0,m_windowRect,WindowFunc,m_title);
}
void WindowFunc(int windowID)
{
常数int border=10;
const int width=50;
const int height=25;
常数int间距=10;
Rect ls=新Rect(
边境
边框+间距,
m_windowRect.width-边框*2,
m_windowRect.高度-边框*2-高度-间距);
//启动滚动视图测试
scrollPosition=GUI.BeginScrollView(ls,scrollPosition,new Rect(0,0,6004800));//如果需要更多的滚动,请增加高度
guiStyle=新的guiStyle();
guiStyle.fontStyle=fontStyle.Bold;
guiStyle.normal.textColor=Color.white;
int myNewsTitlePositionY=边框+间距;//初始位置
if(m_myNewsTitleColl.Length==0){
Rect l=新Rect(边框,myNewsTitlePositionY,m_windowRect.width-边框*2,m_windowRect.height-边框*2-高度-间距);
Label(l,“当前没有新闻,请稍后再试…”,guiStyle);//这是新闻标题标签
}
否则{
对于(var i=0;i
面板必须如图所示(我的蓝色图纸假定为面板-其中标签和按钮必须固定在一起):
另外,我如何使我的模式弹出背景更暗,有没有办法使其不透明,或者我可以为这个模式GUI设置不透明值