unityc string.format用法怎么写路径

1030人阅读
3.3 Unity(41)
看到一个不错的编辑器路径设置输入框,前后都有文本提示,输入只在中间进行输入。效果如下所示:
默认的时候:
输入的时候:
代码如下:
using UnityE
using UnityE
public class TestWindow : EditorWindow
[MenuItem("Tools/Test")]
static void Init()
var window = EditorWindow.GetWindow&TestWindow&();
window.titleContent.text = "路径设置";
window.Show();
private string m_P
void OnGUI()
EditorGUILayout.LabelField("Path:");
m_Path = RelativeAssetPathTextField(m_Path, ".prefab");
public static GUIStyle TextFieldRoundE
public static GUIStyle TextFieldRoundEdgeCancelB
public static GUIStyle TextFieldRoundEdgeCancelButtonE
public static GUIStyle TransparentTextF
private string RelativeAssetPathTextField(string path, string extension)
if (TextFieldRoundEdge == null)
TextFieldRoundEdge = new GUIStyle("SearchTextField");
TextFieldRoundEdgeCancelButton = new GUIStyle("SearchCancelButton");
TextFieldRoundEdgeCancelButtonEmpty = new GUIStyle("SearchCancelButtonEmpty");
TransparentTextField = new GUIStyle(EditorStyles.whiteLabel);
TransparentTextField.normal.textColor = EditorStyles.textField.normal.textC
Rect position = EditorGUILayout.GetControlRect();
GUIStyle textFieldRoundEdge = TextFieldRoundE
GUIStyle transparentTextField = TransparentTextF
GUIStyle gUIStyle = (path != "") ? TextFieldRoundEdgeCancelButton : TextFieldRoundEdgeCancelButtonE
position.width -= gUIStyle.fixedW
if (Event.current.type == EventType.Repaint)
GUI.contentColor = (EditorGUIUtility.isProSkin ? Color.black : new Color(0f, 0f, 0f, 0.5f));
textFieldRoundEdge.Draw(position, new GUIContent("Assets/"), 0);
GUI.contentColor = Color.
Rect rect =
float num = textFieldRoundEdge.CalcSize(new GUIContent("Assets/")).x - 2f;
rect.y += 1f;
rect.width -=
EditorGUI.BeginChangeCheck();
path = EditorGUI.TextField(rect, path, transparentTextField);
if (EditorGUI.EndChangeCheck())
path = path.Replace('\\', '/');
if (Event.current.type == EventType.Repaint)
Rect position2 =
float num2 = transparentTextField.CalcSize(new GUIContent(path + ".")).x - EditorStyles.whiteLabel.CalcSize(new GUIContent(".")).x; ;
position2.x += num2;
position2.width -= num2;
GUI.contentColor = (EditorGUIUtility.isProSkin ? Color.black : new Color(0f, 0f, 0f, 0.5f));
EditorStyles.label.Draw(position2, extension, false, false, false, false);
GUI.contentColor = Color.
position.x += position.
position.width = gUIStyle.fixedW
position.height = gUIStyle.fixedH
if (GUI.Button(position, GUIContent.none, gUIStyle) && path != "")
path = "";
GUI.changed = true;
GUIUtility.keyboardControl = 0;
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2755636次
积分:30412
积分:30412
排名:第126名
原创:383篇
转载:88篇
评论:3310条
联系方式:
(1)(1)(1)(3)(1)(1)(1)(1)(1)(1)(1)(2)(1)(1)(1)(1)(1)(3)(1)(1)(1)(1)(2)(1)(3)(3)(3)(1)(1)(3)(1)(1)(8)(1)(3)(2)(2)(3)(2)(3)(2)(1)(1)(2)(3)(6)(1)(4)(3)(1)(3)(5)(5)(5)(5)(1)(3)(5)(4)(4)(4)(5)(5)(1)(13)(11)(7)(5)(2)(5)(4)(5)(2)(7)(14)(18)(23)(19)(5)(35)(22)(21)(8)(10)(42)(49)Unity读取本地图片资源
& 我们以Unity读取本地图片资源为例,总结三种读取方法:
1.采用Resource.Load方法读取,读取在Unity中Assets下Resources目录下的资源名,注意不采用后缀名。(意思是Load方法直接在Resources目录下找资源,路径已经指定)。
&&&&&&&&&&
&&&&&&&&&&&
//加载图片方式1;(图片要放入在Assets/Resources/目录下);
&&&&&&&&&&&
Texture2D _tex = (Texture2D)Resources.Load("Lighthouse");
2.采用WWW类加载资源,此WWW类可以加载网络资源(http://格式),文件协议资源(flie://格式),ftp格式等等。
&&&&&&&&&&&
//加载图片方式2;(可以加载网络服务器和本地图片);
&&&&&&&&&&&
string filePath = "file://" + Application.dataPath +
@"/_Image/grid.png";
&&&&&&&&&&&
WWW www = new WWW(filePath);
&&&&&&&&&&&
3.采用C#的Image类进行图片的加载,获取Image类中的图片数据,为Unity中Texture2D的数据填充。注意此种方式可能出现的问题:
Assets/_Script/AddObjBtnEvent.cs(57,20): error CS0234: The type
or namespace name `Drawing' does not exist in the namespace
`System'. Are you missing an assembly reference?
解决方法之一:
D:\Program
Files\Unity\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll将此路径下的System.Drawing.dll拖入到Project面板层次下,即可编译通过。
&&&&&&&&&&&
//加载图片方式3;
&&&&&&&&&&&
filePath = Application.dataPath + @"/_Image/grid.png";
&&&&&&&&&&&
FileStream fs = new
FileStream(filePath,FileMode.Open,FileAccess.Read);
&&&&&&&&&&&
System.Drawing.Image img =
System.Drawing.Image.FromStream(fs);
//System.Drawing.Image.FromFile(filePath); //方法二加载图片方式。
&&&&&&&&&&
&&&&&&&&&&&
MemoryStream ms = new MemoryStream();
&&&&&&&&&&&
img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
&&&&&&&&&&&
Texture2D _tex2 = new Texture2D(128, 128);
&&&&&&&&&&&
_tex2.LoadImage(ms.ToArray());
&&&&&&&&&&&
//此处为GameObject的材质类附上读取的纹理;
&&&&&&&&&&&
_newObj.renderer.material.mainTexture = _tex2;
转载:http://wuzhouyi2012./blog/static//
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Unity3D截取界面任意位置生成图片并保存 - 博客频道 - CSDN.NET
分类:Unity3D
从事Unity3D开发工作有小一年了吧。这一年里积累了很多开发的经验,做了很多有用的和没有用的东西。
今天开了这个博客,希望能把这些东西慢慢记录下来。
一来可以当做自己的学习笔记见证自己的成长,二来可以和广大的开发工作者一起交流,学习经验。
这次研究的是一个屏幕截取并保存的功能。先上图。
1.Ctrl + Alt + X 开始截图。
2.Enter键执行截屏并弹出保存框。
3.截取后保存的图片。
大致的思路是这样:
1.GL类执行界面选框的显示。
2.用Texture2D类设置新的像素块颜色。
3.System.Windows.forms.dll弹出保存对话框,保存图片文件并打开。
难点在于GL框的绘制逻辑以及新的图片生成时屏幕坐标的计算。
代码如下:
ScreenShot类
using UnityE
using System.C
using System.Windows.F
using System.IO;
using System.T
//截屏工具
public class ScreenShot : MonoBehaviour
//选框材质
//初始位置
Vector3 startPos = Vector3.
//结束位置
Vector3 endPos = Vector3.
//是否绘制
bool isDraw =
//是否清除
bool isClear =
void Update ()
//左键按下开始绘制
if(Input.GetMouseButtonDown(0))
startPos = Input.mouseP
//左键松开GL框悬停
if(Input.GetMouseButtonUp(0))
//鼠标原位置点击不做GL绘制
if(endPos == startPos)
//右键按下重新绘制
if(Input.GetMouseButtonDown(1))
//Enter键按下执行截屏
if(Input.GetKeyDown(KeyCode.Return) && isDraw && !isClear)
StartCoroutine(DoScreenShot(new Vector2(startPos.x,UnityEngine.Screen.height - startPos.y),new Vector2(endPos.x,UnityEngine.Screen.height - endPos.y)));
void OnGUI ()
//文本提示
if(!isDraw)
GUI.Label(new Rect(Input.mousePosition.x,UnityEngine.Screen.height - Input.mousePosition.y,100,100),&
请框选截图范围&);
if(isDraw && !isClear && endPos != startPos)
GUI.Label(new Rect(Input.mousePosition.x,UnityEngine.Screen.height - Input.mousePosition.y,100,100),&
按下Enter键截屏&);
void OnPostRender ()
//绘制GL线框
if(!isDraw)
if(isClear)
endPos = Input.mouseP
GL.PushMatrix();
mat.SetPass(0);
GL.LoadPixelMatrix();
//绘制GL填充面
GL.Begin(GL.QUADS);
GL.Color(new
Color(Color.yellow.r,Color.yellow.g,Color.yellow.b,0.1f));
GL.Vertex3(startPos.x,startPos.y,0);
GL.Vertex3(endPos.x,startPos.y,0);
GL.Vertex3(endPos.x,endPos.y,0);
GL.Vertex3(startPos.x,endPos.y,0);
//绘制GL边框线
GL.Begin(GL.LINES);
GL.Color(Color.red);
GL.Vertex3(startPos.x,startPos.y,0);
GL.Vertex3(endPos.x,startPos.y,0);
GL.Vertex3(endPos.x,startPos.y,0);
GL.Vertex3(endPos.x,endPos.y,0);
GL.Vertex3(endPos.x,endPos.y,0);
GL.Vertex3(startPos.x,endPos.y,0);
GL.Vertex3(startPos.x,endPos.y,0);
GL.Vertex3(startPos.x,startPos.y,0);
GL.PopMatrix();
IEnumerator DoScreenShot (Vector2 startPos,Vector2 endPos)
Debug.Log(new Rect(startPos.x & endPos.x ? startPos.x : endPos.x,startPos.y & endPos.y ? UnityEngine.Screen.height - endPos.y : UnityEngine.Screen.height - startPos.y,Mathf.Abs(endPos.x - startPos.x - 2),Mathf.Abs(endPos.y - startPos.y - 2)));
yield return new WaitForSeconds(1);
Texture2D tex = new Texture2D((int)Mathf.Abs(endPos.x - startPos.x - 2),(int)Mathf.Abs(endPos.y - startPos.y - 2),TextureFormat.RGB24,false);
tex.ReadPixels (new Rect(startPos.x & endPos.x ? startPos.x : endPos.x,startPos.y & endPos.y ? UnityEngine.Screen.height - endPos.y : UnityEngine.Screen.height - startPos.y,Mathf.Abs(endPos.x - startPos.x - 2),Mathf.Abs(endPos.y - startPos.y - 2)),0,0);
tex.Apply();
Destroy(tex);
SaveScreenShot(tex);
//保存文件
void SaveScreenShot (Texture2D tex)
//先将文件保存在工程目录下(其实可以不用这么做)
byte[] bytes = tex.EncodeToPNG();
string filename = System.DateTime.Now.Year.ToString(&0000&) + System.DateTime.Now.Month.ToString(&00&) + System.DateTime.Now.Day.ToString(&00&) + System.DateTime.Now.Hour.ToString(&00&) + System.DateTime.Now.Minute.ToString(&00&) + System.DateTime.Now.Second.ToString(&00&) + Random.Range(0,9999).ToString(&0000&);
string oldFilePath = UnityEngine.Application.dataPath + &/& + filename + &.png&;
FileStream f =new FileStream(oldFilePath,FileMode.Create);
f.Write(bytes,0,bytes.Length);
f.Flush();
f.Close();
System.GC.Collect();
Resources.UnloadUnusedAssets();
//弹出文件保存对话框,调用的是WINDOWS的DLL.
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = &png|*.png&;
string newFilePath = &&;
if (dialog.ShowDialog() == DialogResult.OK)
newFilePath = dialog.FileN
if(newFilePath != &&)
if(File.Exists(newFilePath))
File.Delete(newFilePath);
File.Move(oldFilePath, newFilePath);
UnityEngine.Application.OpenURL(newFilePath);
File.Delete(oldFilePath);
ScreenShotControl类
using UnityE
using System.C
//截屏控制
public class ScreenShotControl : MonoBehaviour
// Use this for initialization
void Start ()
screenshot = GetComponent&ScreenShot&();
// Update is called once per frame
void Update ()
void OnGUI ()
//Ctrl + Alt + X 执行截屏
if(Event.current.Equals(Event.KeyboardEvent(&^&X&)))
screenshot.enabled =
阐述几个开发中的问题:
1.调用Windows的DLL,需要在Unity里更改一下设置。
File -& Build Settings -& Player Settings -&Other Settings -& Optimization -& Api Compatibility Level 更改为 .NET 2.0。
2.在执行完截屏操作后,虽然能够成功生成图片,但是会报一个错。
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.
如果有高手知道具体的解决办法,请指教一下。
3.程序的代码结构写的不是太好,请高手指正。
工程包地址:
排名:千里之外

我要回帖

更多关于 string.format补零 的文章

 

随机推荐