Unity 疑难杂症 持续更新ing

1.类歧义

同一个类名,在不同的命名空间

`Object` is an ambiguous reference between `UnityEngine.Object` and `object`

在引用 using UnityEngine;using System; 会产生这种冲突,使用下面代码决定使用哪个类作为默认类
using Object = System.Object;

2. MTLPixelFormat 错误

当选择iOS模拟器时,会在xcode中报错
iOS Build Error “Typedef redefinition with different types ('NSUInteger' (aka 'unsigned long') vs 'enum MTLPixelFormat')”
直接注释掉即可

//typedef NSUInteger MTLPixelFormat;
//enum
//{
//    MTLPixelFormatBGRA8Unorm,
//    MTLPixelFormatBGRA8Unorm_sRGB,
//};

3.第一次发布ios后设置

  • 需要自己手动设置xcode的 store icon (1024x1024)
3.1 如果完全不包含广告则需要在unity生成xcode项目后,手动删掉某些代码

DeviceSettings.mm文件 QueryASIdentifierManager 函数直接返回nil

4. Debug.Log 输出多数据

尽量不要把Debug.Log单多扩展出来,因为Debug.Log可以定位到代码当前位置,二次扩展后无法达到效果

//字符串拼接
Debug.Log("Mouse position: " + moveDirection + "   Char position: " + playerPosition);
//字符串转化
Debug.Log(string.Format("Mouse Position: {0} Char Position: {1}", moveDirection, playerPosition));
//使用格式化输出
Debug.LogFormat("{0},{1},{2}",color, index, num);
//扩展类 (不推荐,log不会定位到正确的代码语句)
public static void Trace(params object[] v){
     string o="";
     for ( int i = 0; i < v.Length; i++ ){
         o    += ",";
         o    += v[i].ToString();
     }
     Debug.Log(o);
 }

5. super类

继承的父类在c#中是base,而不是super。

6. 如何获得GameObject的大小,以及如何按照指定方向缩放

获取的场景中的长度(图片换算场景长度再乘以缩放大小)

progressBarSizeX = progressBar.GetComponent().sprite.bounds.size.x*progressBar.transform.localScale.x;

按照最左点向右缩放

之所以需要这个是因为要制作进度条,默认对称缩放效果不好

1. 先用上面代码获取到 progressBarSizeX 大小
2. 获取对象的原始位置坐标点
3. 将这些内容保存到内部变量中
4. 每次修改缩放值也同时修改x坐标位置
percent = 0~1 (百分比进度)
scaleX = percent;
positionX = positionXOld + (1-percent)* sizeX * .5f;

7. 如何将UI内容和游戏内容很好的融合在一起

https://www.jianshu.com/p/96fd1fbe8409

8.如何获得某方向的可能检测的点

层混合, 比如想得到id为8,9层的检测,则层混合中写入
1<<8 | 1<<9

RaycastHit2D[] poolHit2D = Physics2D.RaycastAll(
                开始位置点
                , 点(对应方向)
                , 长度(不填则无限长)
                , 层混合);

遍历所有处理

if (poolHit2D.Length > 0) {
    Debug.Log("detect: "+poolHit2D.Length);
    for (int i = 0; i < poolHit2D.Length; i++) {
        Debug.Log("detect: "+poolHit2D [i].collider.gameObject);
    }
}

9.缩放后的大小

在2d世界中大小这个值存放在sprite中, 这样获得

var size = gameObject.GetComponent().sprite.bounds.size;

这个size 不是一个像素大小,是在unity中点长度,他是在scale为1的时候的大小, 所以如果你想获得他在场景中运行时的大小,则需要这样写:

var scale = gameObject.transform.localScale;
var realSize = new Vector2(size.x*scale.x,size.y*scale.y);

你可能感兴趣的:(Unity 疑难杂症 持续更新ing)