首页技术substring怎么读?shell substr

substring怎么读?shell substr

编程之家2026-07-021079次浏览

老铁们,大家好,相信还有很多朋友对于substring怎么读和shell substr的相关问题不太懂,没关系,今天就由我来为大家分享分享substring怎么读以及shell substr的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!

substring怎么读?shell substr

unity sprite怎么获取切割后的图

方法如下:

假设有一张png/tga图集,导入到Unity,放置目录"Assets/Resources/UI"(UI文件夹可替换成其他的,重要的是要在"Assets/Resources/"路径下),默认为如下设置:

为了可以使用Unity自带的精灵切割,要将纹理类型改成"Sprite","Sprite Mode"改成"Multiple","Format"改成"Truecolor",点击"Apply"按钮进行应用。

接着,点击"Sprite Editor"打开精灵编辑器,点击左上角的"Slice"按钮,弹出切片设置,再次点击里面的"Slice"按钮,就会自动对图片进行切割,如下图所示:

在对切割不完整的地方进行修正后,点击右上角的"Apply"按钮,进行保存。可以看到Project视图下这个图集,已经被分割出许多小图了,如下图所示:

接下来,因为要对图片进行读写操作,要更改图片的属性才能进行,否则会提示如下:

substring怎么读?shell substr

UnityException: Texture'testUI' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

将图片纹理类型更改为"Advanced",将"Read/Write Enabled"属性进行打勾,如下图所示:

创建一个脚本文件,代码如下:

using UnityEngine;

using UnityEditor;

public class TestSaveSprite

substring怎么读?shell substr

{

(MenuItem("Tools/导出精灵"))

static void SaveSprite()

{

string resourcesPath="Assets/Resources/";

foreach(Object obj in Selection.objects)

{

string selectionPath= AssetDatabase.GetAssetPath(obj);

//必须最上级是"Assets/Resources/"

if(selectionPath.StartsWith(resourcesPath))

{

string selectionExt= System.IO.Path.GetExtension(selectionPath);

if(selectionExt.Length== 0)

{

continue;

}

//从路径"Assets/Resources/UI/testUI.png"得到路径"UI/testUI"

string loadPath= selectionPath.Remove(selectionPath.Length- selectionExt.Length);

loadPath= loadPath.Substring(resourcesPath.Length);

//加载此文件下的所有资源

Sprite()sprites= Resources.LoadAll<Sprite>(loadPath);

if(sprites.Length> 0)

{

//创建导出文件夹

string outPath= Application.dataPath+"/outSprite/"+ loadPath;

System.IO.Directory.CreateDirectory(outPath);

foreach(Sprite sprite in sprites)

{

//创建单独的纹理

Texture2D tex= new Texture2D((int)sprite.rect.width,(int)sprite.rect.height, sprite.texture.format, false);

tex.SetPixels(sprite.texture.GetPixels((int)sprite.rect.xMin,(int)sprite.rect.yMin,

(int)sprite.rect.width,(int)sprite.rect.height));

tex.Apply();

//写入成PNG文件

System.IO.File.WriteAllBytes(outPath+"/"+ sprite.name+".png", tex.EncodeToPNG());

}

Debug.Log("SaveSprite to"+ outPath);

}

}

}

Debug.Log("SaveSprite Finished");

}

}

在Unity编辑器将会看到Tools菜单下多了"导出精灵"项,选中图集,然后点击"导出精灵"菜单项,即可导出子图成功。如下图所示:

C#StartIndex 不能小于 0 要怎么修改

string tmp="";

if(FileType.Length> 1)

tmp= FileType.Substring(1,FileType.Length-1);

int start= FCKeditor1.Value.IndexOf(UploadPath);

if(start< 0)

start= 0;

int end= FCKeditor1.Value.IndexOf(tmp)- FCKeditor1.Value.IndexOf(UploadPath);

if(end< 0) end= 0;

imgURL="/"+ FCKeditor1.Value.Substring(start,end)+ tmp;

另外,这个代码是你写的吗?太长导致可读性太差,建议一行代码不超过80字符

汗一个先,这个错误就是在一些截取字符串的地方报错,至于具体是哪里出错,没有看到你的代码,不可能指出来的,另外,告诉你这么多,你也应该能自己解决了才对。

你的代码没有给全,应该是你在取扩展名时出的问题,

你取扩展名应该是类似下面的代码:

string FileType= fileName.Substring(fileName.IndexOf("."));

此时如果上传一个没有扩展名的文件,那么fileName.IndexOf(".")就等于-1,而fileName.Substring(-1)当然就会报错:StartIndex不能小于 0。

解决方法就是先判断一下有没有扩展名。

substring怎么读的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于shell substr、substring怎么读的信息别忘了在本站进行查找哦。

borderstyle属性是干嘛的(position属性)网页动态特效代码(html动态网页效果代码)