Unity3d与c#

一些函数与功能(不定时更新)

1:byte数组转string输出

byte[] result=new byte[]{1,2,3,4};
string vstr = "{" + string.Join(",", result) + "}";
Debug.Log(vstr); 

2:sbyte转byte

sbyte[] temp = new sbyte[]{ 8, -25, 7} ;
byte[] temp2 = new byte[temp.Length];
Buffer.BlockCopy(temp, 0, temp2, 0, temp.Length);

3:在脚本中实例化Button类型预制体并添加onclick()函数

public GameObject car;//设置预制体

GameObject Car = Instantiate(car).gameObject;//实例化
newCar.GetComponent<RectTransform>().SetParent(this.GetComponent<RectTransform>());//设置父节点
Car.GetComponent<Button>().onClick.AddListener(() => { run(); });//添加onclick函数run()

4:判断物体是第几个子物体

int index = this.transform.GetSiblingIndex();

5:依据name寻找子物体

string name = "Car";
GameObject obj = this.transform.Find(name).gameObject;

6:int转string

string name = Convert.ToString(index);

8:image转byte

private byte[] ImageToBytes(string imageFileName){
  return File.ReadAllBytes(imageFileName);
}

9:byte[ ] 转 texture

 private static Texture2D BytesToImage(byte[] bytes,int w=800,int h=561)
 {
        Texture2D texture2D = new Texture2D(w, h);
        texture2D.LoadImage(bytes);
        return texture2D;
 }

10:img,byte[ ],sprite

public Image Img;
public Sprite oldSprite;
public Sprite newSprite;
private string imageName;
void Start()
{
   imageName = "Assets/Img/" + "斋藤飞鸟.jpg";//原图片地址,jpg格式
   Img.sprite = oldSprite;//设置ui的sprite
   byte[] x = ImageToBytes(imageName);//图片转byte[]
   Debug.Log(x.Length);//用于判断是否转化成功,能输出则表示成功
   Texture2D texture2D = BytesToImage(x);//byte[]转texture2D
   newSprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);//根据texture2D设置sprite
   Img.sprite=newSprite;//给ui贴图
}

11:当退出程序时,如果要执行一些代码,可以使用OnApplicationQuit(),该函数为unity默认函数

private void OnApplicationQuit()
{
	Debug.Log("我在这!!!");
}

12:OnApplicationQuit()函数只能在unity的编辑器条件下使用,如果要导出apk,在安卓环境下运行,需要将OnApplicationQuit()替换为OnApplicationPause()

private void OnApplicationPause()
{
	Debug.Log("我在这!!!");
}

13:c#中两个list不能直接copy,不然会出错,笔者就是因为直接拷贝出现了bug,可以通过for循环依次copy每个元素,不过笔者更推荐以下用法

//将carsNumber中的元素复制到CarsNumber中
CarsNumber.clear()
carsNumber.ForEach(i => CarsNumber.Add(i));

14:unity导出Pc端Apk时,按下Esc退出

void Update()
{
    ExitByEsc();
}
public void ExitByEsc()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        //测试时不能执行,打包后可以执行
        //Application.Quit();
        //打包时不能执行
        UnityEditor.EditorApplication.isPlaying = false;
     }
}

15:设置rectTransform的位置

//VideoPlay是一个obj
VideoPlay.transform.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(8f, -767f, 0f);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

5436649486

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值