Unity技术分享程序员技术干货

Unity 打包时修改场景

2016-06-23  本文已影响1442人  2b75747cf703

遇到一个需求,同一套代码要同时处理横竖屏,而且每款产品用的图片不一样。
一般会选择把资源放在Resources下面,然后动态加载。。
但是。。我们公司的产品不大,数量却很多,如果这样搞的话,打包时多出很多“无用资源”。

所以,利用Unity提供的PostProcessScene回调属性。横屏产品删除竖屏节点,竖屏的产品删除横屏节点。而且能够做到只打包有用的资源。

http://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html

Paste_Image.png Paste_Image.png
    /// <summary>
    /// 是否竖屏模式
    /// </summary>
    public static bool IsPortait
    {
        get
        {
#if UNITY_EDITOR
            if(Application.isPlaying)
                return Screen.height > Screen.width;
            else
            {
                switch (PlayerSettings.defaultInterfaceOrientation)
                {
                case UIOrientation.Portrait:
                case UIOrientation.PortraitUpsideDown:
                    return true;

                case UIOrientation.AutoRotation:
                    return PlayerSettings.allowedAutorotateToPortrait || PlayerSettings.allowedAutorotateToPortraitUpsideDown;
                }

                return false;
            }
#else
            return Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown;
#endif
        }
    }
using UnityEngine;
using UnityEditor.Callbacks;
using Babybus.Builder;
using UnityEngine.UI;
using UnityEditor;
using Babybus.BabyModule;

namespace Babybus.Framework.Extension
{
    class MyScenePostprocessor
    {
        [PostProcessScene]
        public static void OnPostprocessScene()
        {
            var objects = Resources.FindObjectsOfTypeAll<LearningSystem>();
            if (objects == null || objects.Length == 0)
                return;

            Transform learningSystem = null;

            foreach (var item in objects)
            {
                if (AssetDatabase.GetAssetPath(item) == "")
                {
                    learningSystem = item.transform;
                    break;
                }
            }

            if (learningSystem == null)
                return;

            if (BabySystem.IsPortrait)
            {
                Object.DestroyImmediate(learningSystem.Find("横屏").gameObject);

                //竖屏目前只有一款,不需要做额外处理
            }
            else
            {
                Object.DestroyImmediate(learningSystem.Find("竖屏").gameObject);

                var landscape = learningSystem.Find("横屏");

                var uiRootPath = "Assets/BabyFrameWork/UI/";
                landscape.Find("能力体系").GetComponent<Image>().sprite = AssetDatabase.LoadAssetAtPath<Sprite>(uiRootPath + "能力体系/" + BabySystem.productName + ".png");
                landscape.Find("饼图").GetComponent<Image>().sprite = AssetDatabase.LoadAssetAtPath<Sprite>(uiRootPath + "饼图/" + BabySystem.productName + ".png");
                landscape.Find("学习目标/图标").GetComponent<Image>().sprite = AssetDatabase.LoadAssetAtPath<Sprite>(uiRootPath + "学习目标图标/" + BabySystem.nextLearnProductName + ".png");
            }
        }
    }
}

其中LearningSystem组件挂在如下图的“博士帽-体系”节点上。

Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读