Unity3d 周分享(14期 2019.4.1 )

这篇博客汇总了近期Unity3d的相关技术分享,涵盖了UI.Button事件复制、UI.Image绘图、AssetDatabase资产类型获取、Unity日志错误处理、Inspector旋转角度解析、Unity开源特性探索等多个方面。此外,还分享了Unity编辑器扩展、自动化测试、内存管理、性能优化和崩溃报告等方面的实用技巧和资源链接。
摘要由CSDN通过智能技术生成

选自过去1~2周 自己所看到外文内容:https://twitter.com/unity3d 和各种其他博客来源吧   

 

1、 看到一个帖子: https://qiita.com/chocho/items/51b65c2601c67e5cc6d2

通过脚本克隆UI.Button时,也会复制onClick事件 ! 具体说是:

1).如果在脚本中完成AddListener()。(这个将不是永久性的)

onClick事件未复制。不能接管

2).从Inspector 面板上进行事件绑定时。(这个将是永久的)

onClick事件也被复制。被接管

想要拷贝之后删除原来的监听 就要关闭:

int persistentEventCount = btn.onClick.GetPersistentEventCount();
for (int i = 0; i < persistentEventCount; i++)
    btn.onClick.SetPersistentListenerState(i, UnityEventCallState.Off);

 

https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.Button.ButtonClickedEvent.html

https://docs.unity3d.com/jp/current/ScriptReference/Events.UnityEventCallState.html

https://docs.unity3d.com/jp/current/ScriptReference/Events.UnityEventBase.GetPersistentEventCount.html

 

 

2、 在UI.Image上绘画 一条线。

https://qiita.com/chocho/items/d5125ed051b22e95709c

 

 

 

3、 获取[Unity] AssetDatabase中的所有资产类型名称

public Type[] GetAllAssetClassTypeInfo()
{
    return AssetDatabase.FindAssets("t:object").Select(guid => new
    {
        var assetPath = AssetDatabase.GUIDToAssetPath(guid);
        return AssetDatabase.GetMainAssetTypeAtPath(assetPath);
    }).Distinct();       // 重复的类型信息将不会在Distinct中重复
}

 

 

 

4、 当Unity的Log 中发生错误类型时, 打开一个窗口:

public class ErrorReceivedWindow : EditorWindow
{
   [InitializeOnLoadMethod]
   private static void _onInit()
   {
        Application.logMessageReceived += ApplicationOnLogMessageReceived;
   }
   private static void ApplicationOnLogMessageReceived(string condition, string stacktrace, LogType type)
   {
       if (type == LogType.Exception)
       {
           GetWindow<ErrorReceivedWindow>();
       }
   }
}

 

 

 

5、 原来 Inspector上显示的 Rotation角度并不一定就是 。 transform.localEulerAngles

我之前以为就是一个值。

 

 

Debug.LogError(" " + transform.localEulerAngles);

Debug.LogError(" " + transform.localRotation.eulerAngles);

UnityEditor.SerializedObject serializedObject = new UnityEditor.SerializedObject(transform);

UnityEditor.SerializedProperty serializedEulerHint = serializedObject.FindProperty("m_LocalEulerAnglesHint");

Debug.LogError(serializedEulerHint.vector3Value);

参考帖子: https://forum.unity.com/threads/how-to-get-euler-rotation-angles-exactly-as-displayed-in-the-transform-inspector-solved.425244/

 

 

 

 

 

 

 

6、 如果查看 Unity开源出来的 UnityCsReference 会发现一个 不知道的隐藏功能。我们来介绍一个例子。

https://connect.unity.com/p/unitycsreferencededuo-kunozhi-shi-woqu-riru-reyou

AboutWindow类有一个名为 ListenForSecretCodes的方法。当我阅读代码时...在窗口中输入Internal似乎切换到InternalMode 。 如果退出这个模式 就在输入一下 Internal

然后就会出现一些菜单:

 

 

 

7、【Unity】示例编辑器扩展,以获取Unity项目中包含的所有场景文件的路径

        var list = AssetDatabase
            .FindAssets( "t:scene" )
            .Select( AssetDatabase.GUIDToAssetPath )
        ;
// 或者  
        var list = AssetDatabase
            .GetAllAssetPaths()
            .Where( c => c.EndsWith( ".unity" ) )
        ;

 

 

[Unity]“EditorSceneManager.GetSceneManagerSetup”,可以获取 层次结构中存在的所有场景的信息

    [MenuItem( "Tools/Hoge" )]
    private static void Hoge()
    {
        var list = EditorSceneManager.GetSceneManagerSetup();

        foreach ( var n in list )
        {
            var sb = new StringBuilder();
            sb.AppendLine( $"path: {n.path}" );
            sb.AppendLine( $"isLoaded: {n.isLoaded}" );
            sb.AppendLine( $"isActive: {n.isActive}" );
            Debug.Log( sb.ToString() );
        }
    }

 

 

 

8、 【Unity】如何将附加到游戏对象的所有MonoBehaviour的信息输出为JSON

using System.Linq;
using UnityEngine;
public class Example : MonoBehaviour
{
    private void Start()
    {
        var list = GetComponents<MonoBehaviour>()
            .Select( c => JsonUtility.ToJson( c, true ) )
        ;

        var str = string.Join( "\n", list );

        Debug.Log( str );
    }
}

 

 

9、 [Unity]扩展方法,当从脚本中切换uGUI的isOn时,不希望触发onValueChanged

using UnityEngine.UI;
public static class ToggleExt
{
    public static void SetIsOnWithoutCallback( this Toggle self, bool isOn )
    {
        var onValueChanged = self.onValueChanged;
        self.onValueChanged = new Toggle.ToggleEvent();
        self.isOn = isOn;
        self.onValueChanged = onValueChanged;
    }
}
            怎么用?
var toggle = GetComponent<Toggle>();
toggle.isOn = true;                    // onValueChanged 触发
toggle.SetIsOnWithoutCallback( true ); // onValueChanged 不触发

 

 

10、 [Unity]如何创建可在创建新项目时选择的自定义模板

http://baba-s.hatenablog.com/entry/2019/03/18/090000

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值