GUI自适应屏幕
public override void GUIDraw (){
//开始缩放
//先定义一个标准屏幕大小代码按照这个分辨率写,这里屏是800*480象素,GUI矩阵再乘以新的缩放矩阵实现GUI的缩放,GUI.matrix*=scaleMatrix;
ScreenScaleUtil.BeginScreenScale ();
//GUI代码
//结束缩放
ScreenScaleUtil.EndScreenScale ();
}
ScreenScaleUtil.cs
//开始缩放
//先定义一个标准屏幕大小代码按照这个分辨率写,这里屏是800*480象素,GUI矩阵再乘以新的缩放矩阵实现GUI的缩放,GUI.matrix*=scaleMatrix;
ScreenScaleUtil.BeginScreenScale ();
//GUI代码
//结束缩放
ScreenScaleUtil.EndScreenScale ();
}
ScreenScaleUtil.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum ScreenScaleType
{
None,
Center,
Fit,
Full
}
public static class ScreenScaleUtil
{
static ScreenScaleUtil ()
{
scaleRate=1;
scaleType = ScreenScaleType.None;
scaleMatrix = Matrix4x4.identity;
screenSize = new Vector2 (800, 480);
ScaleType=ScreenScaleType.Fit;
lstMats =new List<Matrix4x4>();
}
private static Matrix4x4 scaleMatrix;
private static ScreenScaleType scaleType;
private static Vector2 screenSize;
private static float scaleRate;
public static Matrix4x4 ScaleMatrix {
get {return scaleMatrix; }
}
public static ScreenScaleType ScaleType {
get { return scaleType; }
set {
if (value != scaleType) {
scaleType = value;
private static Vector2 screenSize;
private static float scaleRate;
public static Matrix4x4 ScaleMatrix {
get {return scaleMatrix; }
}
public static ScreenScaleType ScaleType {
get { return scaleType; }
set {
if (value != scaleType) {
scaleType = value;
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
}
}
public static Vector2 ScreenSize {
get { return screenSize; }
set {
if (value != screenSize) {
screenSize = value;
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
}
}
}
}
public static Vector2 ScreenSize {
get { return screenSize; }
set {
if (value != screenSize) {
screenSize = value;
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
}
}
public static float ScaleRate{
get{
return scaleRate;
}
}
public static void Update(){
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType)
{
public static float ScaleRate{
get{
return scaleRate;
}
}
public static void Update(){
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType)
{
float rate;
return GetScreenScaleMatrix(screenSize,scaleType,out rate);
}
public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType,out float rate)
{
Matrix4x4 scaleMatrix;
rate = 1;
if (scaleType == ScreenScaleType.None) {
scaleMatrix = Matrix4x4.identity;
} else {
return GetScreenScaleMatrix(screenSize,scaleType,out rate);
}
public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType,out float rate)
{
Matrix4x4 scaleMatrix;
rate = 1;
if (scaleType == ScreenScaleType.None) {
scaleMatrix = Matrix4x4.identity;
} else {
if ((Screen.width / (float)Screen.height) < (screenSize.x / screenSize.y)) {
if (scaleType == ScreenScaleType.Fit) {
rate = Screen.width / screenSize.x;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.height / screenSize.y;
}
} else {
if (scaleType == ScreenScaleType.Fit) {
rate = Screen.height / screenSize.y;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.width / screenSize.x;
}
}
scaleMatrix = Matrix4x4.TRS (new Vector3 ((Screen.width - screenSize.x * rate) /
if (scaleType == ScreenScaleType.Fit) {
rate = Screen.width / screenSize.x;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.height / screenSize.y;
}
} else {
if (scaleType == ScreenScaleType.Fit) {
rate = Screen.height / screenSize.y;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.width / screenSize.x;
}
}
scaleMatrix = Matrix4x4.TRS (new Vector3 ((Screen.width - screenSize.x * rate) /
2.0f, (Screen.height - screenSize.y * rate) / 2.0f, 0), Quaternion.identity, new Vector3 (rate, rate, 1));
}
return scaleMatrix;
}
private static List<Matrix4x4> lstMats;
public static void BeginScreenScale(){
if(lstMats.Count>0){
return;
}
getsavefilename
}
return scaleMatrix;
}
private static List<Matrix4x4> lstMats;
public static void BeginScreenScale(){
if(lstMats.Count>0){
return;
}
getsavefilename
lstMats.Add(GUI.matrix);
GUI.matrix*=ScaleMatrix;
}
public static void BeginScreenScale(ScreenScaleType scaleType ){
if(lstMats.Count>0){
return;
}
lstMats.Add(GUI.matrix);
GUI.matrix*=GetScreenScaleMatrix(screenSize,scaleType);
}
GUI.matrix*=ScaleMatrix;
}
public static void BeginScreenScale(ScreenScaleType scaleType ){
if(lstMats.Count>0){
return;
}
lstMats.Add(GUI.matrix);
GUI.matrix*=GetScreenScaleMatrix(screenSize,scaleType);
}
public static void EndScreenScale(){
if(lstMats.Count<=0)
return;
GUI.matrix=lstMats[lstMats.Count-1];
lstMats.RemoveAt(lstMats.Count-1);
}
}
1 GUI汇总
function OnGUI() {
GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1
GUI.Box(Rect(1,21,100,20),"I'm a Box"); //2
GUI.Button(Rect(1,41,100,20),"I'm a Button"); //3
GUI.RepeatButton(Rect(1,61,120,20),"I'm a RepeatButton"); //4
GUI.TextField(Rect(1,81,100,20),"I'm a TextFielld"); //5
GUI.TextArea(Rect(1,101,100,40),"I'm a TextArea,\nMultiline"); //6
GUI.Toggle(Rect(1,141,120,20),true,"I'm a Toggle true"); //7
GUI.Toggle(Rect(1,161,120,20),false,"I'm a Toggle false"); //8
GUI.Toolbar(Rect(1,181,160,20),-1,["Toolbar","Tool2","Tool3"); //9
GUI.SelectionGrid(Rect(1,201,190,20),2,["Selection","Grid","select3"],3); //10
GUI.HorizontalSlider(Rect(1,221,180,20),3.0,0,10.0); //11
GUI.VerticalScrollbar(Rect(1,241,20,100),3.0,1,0.0,10.0); //12
//13
GUI.BeginScrollView (Rect (200,10,100,100),, Rect (0, 0, 220, 200));
GUI.Label(Rect(0,0,100,20),"I'm a Label");
GUI.EndScrollView();
//14
GUI.Window(0,Rect(200,129,100,100),funcwin,"window");
}
function funcwin(windowID:int)
{
GUI.DragWindow(Rect(0,0,10000,2000));
}
2 JS调用DLL
import System;
import System.Runtime.InteropServices;
@DllImport("user32.dll")
public static function MessageBox(Hwnd : int,text : String,Caption : String,iType : int) : int {};
function Start()
{
MessageBox(0, "API Message Box", "Win32 API", 64) ;
}
function Update () {
}
3 物体标签
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
var clampToScreen = false; // If true, label will be visible even if object is off screen
var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true; // Use the camera tagged MainCamera
var cameraToUse : Camera; // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;
function Start () {
thisTransform = transform;
if (useMainCamera)
cam = Camera.main;
else
cam = cameraToUse;
camTransform = ansform;
}
function Update () {
if (clampToScreen) {
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
thisTransform.position.z);
}
else {
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
}
}
@script RequireComponent(GUIText)
4 unity3d读取保存xml文件
import System;
import System.Xml;
import System.Xml.Serialization;
import System.IO;
import System.Text;
class CeshiData{
var Ceshi1 : String;
var Ceshi2 : String;
var Ceshi3 : float;
var Ceshi4 : int;
}
class UserData
{
public var _iUser : CeshiData = new CeshiData();
function UserData() { }
}
private var c1 : String;
private var c2 : String;
private var c3 : float;
private var c4 : int;
private var _FileLocation : String;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论