zoukankan      html  css  js  c++  java
  • 3D角色渲染到2D界面上

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using Carrie.Net;
    using CommonUI.Editor.UI;
    using CommonUI_Unity3D.Impl;
    using JsonFx.Json;
    using System;
    
    public class Character3DController : MonoBehaviour, IScene
    {
        public class CharactorCell
        {
            public byte prof;
            public uint ID;
            public GameObject GO;
        }
    
        public AssemblerInfo m_Data_AvatarInfo_SS = new AssemblerInfo(0, NetConstant.CHARACTER_NAME_ARCANEARCHER, NetConstant.CHARACTER_NAME_ARCANEARCHER + "_head_0000", NetConstant.CHARACTER_NAME_ARCANEARCHER + "_body_0000", "", "", "weapon2001", "", "wing_0001");
        public AssemblerInfo m_Data_AvatarInfo_ZS = new AssemblerInfo(0, NetConstant.CHARACTER_NAME_UNKNOW, NetConstant.CHARACTER_NAME_UNKNOW + "_head_0000", NetConstant.CHARACTER_NAME_UNKNOW + "_body_0000", "", "weapon0001", "", "", "wing_0001");
    
        private static Dictionary<byte, CharactorCell> mCharactorCellList = new Dictionary<byte, CharactorCell>();
        private static Dictionary<OpenType, DeployData> mDeployData = new Dictionary<OpenType, DeployData>();
    
        UECanvas mUECanvas;
        Texture2D mTex = null;
        RenderTexture targetTexture;
        UnityImage image;
    
        private static Character3DController m_Instance = null;
    
        void Awake()
        {
            int w = Screen.width;
            Load(w.ToString());
            m_Instance = this;
            SceneManager.Instance.AddListener(this);
        }
    
        void OnApplicationQuit()
        {
            Assembler.GetInstance().OnFinishLoading -= HandleModelProf;
    
            m_Instance = null;
        }
    
        IEnumerator Start()
        {
            Assembler.GetInstance().OnFinishLoading += HandleModelProf;
    
            LoadProfModel(NetConstant.CHARACTER_TYPE_WARRIOR);
            LoadProfModel(NetConstant.CHARACTER_TYPE_THIEF);
            LoadProfModel(NetConstant.CHARACTER_TYPE_ARCANEARCHER);
    
            yield return 0;
    
        }
    
        public void HandleOnEnterScene(SceneManager sender)
        {
    
        }
    
        public void HandleOnLeaveScene(SceneManager sender)
        {
            if (mCharactorCellList != null)
            {
                foreach (CharactorCell cell in mCharactorCellList.Values)
                {
                    if (cell.GO != null)
                    {
                        cell.GO.SetActive(false);
                        UnityEngine.Object.Destroy(cell.GO);
                    }
                }
                mCharactorCellList.Clear();
            }
            Assembler.GetInstance().OnFinishLoading -= HandleModelProf;
        }
    
        /// <summary>
        /// Load Cell For Prof
        /// </summary>
        /// <param name="prof"></param>
        void LoadProfModel(byte prof)
        {
            if (!mCharactorCellList.ContainsKey(prof))
            {
                CharactorCell cell = new CharactorCell();
                cell.prof = prof;
                {
                    AssemblerInfo info = null;
                    switch (prof)
                    {
                        case NetConstant.CHARACTER_TYPE_WARRIOR:
                            info = m_Instance.m_Data_AvatarInfo_ZS;
                            break;
                        case NetConstant.CHARACTER_TYPE_THIEF:
                            break;
                        case NetConstant.CHARACTER_TYPE_ARCANEARCHER:
                            info = m_Instance.m_Data_AvatarInfo_SS;
                            break;
                        case NetConstant.CHARACTER_TYPE_UNKNOW:
                            info = null;
                            break;
                        default:
                            Debug.LogError("prof error");
                            break;
                    }
                    cell.ID = Assembler.GetInstance().Create(info);
                }
                {
                    //  default is null,
                    cell.GO = null;
    
                }
    
                mCharactorCellList.Add(prof, cell);
            }
            else
            {
                Debug.LogWarning("Repeat Add");
            }
        }
    
        /// <summary>
        /// show model for prof
        /// </summary>
        /// <param name="prof"></param>
        void ShowProModel(bool open, byte prof, DeployData deployData)
        {
            if (open)
            {
                m_Camera.SetActive(true);
                m_Model.SetActive(true);
                m_Camera.GetComponent<Camera>().orthographicSize = deployData.size;
            }
            else
            {
                m_Camera.SetActive(false);
                m_Model.SetActive(false);
            }
    
            foreach (CharactorCell cell in mCharactorCellList.Values)
            {
                if (cell.GO != null)
                {
                    if (prof == cell.prof)
                    {
                        cell.GO.SetActive(true);
                        cell.GO.transform.localPosition = new Vector3(deployData.x,deployData.y,0);
                        Play(cell.GO);
                    }
                    else
                        cell.GO.SetActive(false);
                }
            }
    
            m_Camera.camera.Render();
        }
    
        /// <summary>
        /// Udpate GameObject of Cell
        /// </summary>
        /// <param name="open"></param>
        /// <param name="prof"></param>
        /// <param name="info"></param>
        /// <param name="canvas"></param>
        static public void Update3D(bool open, byte prof,OpenType type,AssemblerInfo info = null)
        {
            if (info != null)
            {
                string data = JsonWriter.Serialize(info);
                Debug.Log(open + " " + NetConstant.GetResNameByProf(prof) + " " + data);
            }
            DeployData deployData = mDeployData[type];
            m_Instance.ShowProModel(open, prof,deployData);
          
            ///
            /// if null, info is deafult
            ///
            if (info == null)
            {
                switch (prof)
                {
                    case NetConstant.CHARACTER_TYPE_WARRIOR:
                        info = m_Instance.m_Data_AvatarInfo_ZS;
                        break;
                    case NetConstant.CHARACTER_TYPE_THIEF:
                        break;
                    case NetConstant.CHARACTER_TYPE_ARCANEARCHER:
                        info = m_Instance.m_Data_AvatarInfo_SS;
                        break;
                    case NetConstant.CHARACTER_TYPE_UNKNOW:
                        info = null;
                        break;
                    default:
                        Debug.LogError("prof error");
                        break;
                }
            }
    
    
            if (!open) return;
    
            CharactorCell cell = (CharactorCell)mCharactorCellList[prof];
    
            //
            //  Control 
            //
    //        if (canvas != m_Instance.mUECanvas)
    //        {
    //            m_Instance.mUECanvas = canvas;
    //
    //            int width = (int)canvas.Width;
    //            int height = (int)canvas.Height;
    //            m_Instance.mTex = new Texture2D(width, height);
    //            m_Instance.targetTexture = new RenderTexture(width, height, 24);
    //            m_Instance.m_Camera.camera.targetTexture = m_Instance.targetTexture;
    //
    //            m_Instance.image = new UnityImage(m_Instance.mTex, width, height);
    //            m_Instance.mUECanvas.SetImage(m_Instance.image);
    //        }
    
            Assembler.GetInstance().Update(cell.ID, info);
        }
    
        public GameObject m_Model;
        public GameObject m_Camera;
    
        void HandleModelProf(uint id)
        {
            if (m_Instance == null)
                return;
    
            foreach (CharactorCell cell in mCharactorCellList.Values)
            {
                if (id == cell.ID)
                {
                    //  first create
                    if (cell.GO == null)
                    {
                        cell.GO = Assembler.GetInstance().GetObject(cell.ID);
                        cell.GO.SetActive(true);
                        cell.GO.transform.parent = m_Model.transform;
                        cell.GO.transform.localPosition = Vector3.zero;
                        cell.GO.transform.localEulerAngles = Vector3.zero;
    
                        foreach (Transform t in cell.GO.GetComponentsInChildren<Transform>())
                            if (t.gameObject != null)
                                t.gameObject.layer = m_Model.layer;
                    }
                   // Play(cell.GO);
                }
            }
    
            if (m_Camera!= null && m_Camera.camera != null && m_Camera.camera.enabled)
                m_Camera.camera.Render();
    
        }
    
    
        public delegate void PostRenderDelegate();
        public PostRenderDelegate PostRender;
    
        /// <summary>
        /// update render texutre
        /// </summary>
        public void OnPostRender()
        {
            if (mUECanvas == null)
                return;
    //        m_Camera.GetComponent<Camera>().orthographicSize = cameraSize;
    //        mTex.ReadPixels(new Rect(0, 0, targetTexture.width, targetTexture.height), 0, 0);
    //        mTex.Apply();
        }
    
        #region 播放动画
        private void Play(GameObject obj)
        {
            Animation anim = obj.GetComponent<Animation>();
            if (anim != null)
            {
                AnimationState animState = anim[Bundle_Animation.n_idle];
                if (animState != null)
                    animState.wrapMode = WrapMode.Loop;
    
                anim.Play(Bundle_Animation.n_idle);
    
                PlayAnimation(obj);
            }
        }
    
        void PlayAnimation(GameObject o)
        {
            Transform[] hips = o.GetComponentsInChildren<Transform>();
    
            foreach (Transform hip in hips)
            {
                if (hip.name.Equals("wing"))
                {
                    GameObject wing = hip.gameObject;
                    wing.animation.wrapMode = WrapMode.Loop;
                    wing.animation.Play("n_run");
    
                    break;
                }
            }
        }
        #endregion
      
    
        public void HandlePrepareEnterScene(SceneManager sender, PrepareDone done)
        {
            done(this);
        }
    
        public void HandlePrepareLeaveScene(SceneManager sender, PrepareDone done)
        {
            done(this);
        }
    
        IniParser parser = null;
        float cameraSize = 1.0f;
        public void Load(string width) 
        {
            int wight = Screen.width;
            TextAsset ta = (TextAsset)Resources.Load("Data/test", typeof(TextAsset));
            parser = new IniParser(ta);
            LoadInfo(OpenType.CHARATOR_VIEW, width);
            LoadInfo(OpenType.CHARATOR_PACK, width);
           
        }
        private void LoadInfo(OpenType type,string width) 
        {
            String str = parser.GetSetting(type.ToString(), width);
            string[] strArray = str.Split(new char[] { ',' });
            DeployData data = new DeployData();
            data.x = float.Parse(strArray[0]);
            data.y = float.Parse(strArray[1]);
            data.size = float.Parse(strArray[2]);
            mDeployData.Add(type, data);
        }
    
        //3D人物使用配置数据
        private void SetCameraSize(float value) 
        {
            m_Camera.GetComponent<Camera>().orthographicSize = value;
        }
    
        private void SetCharactorPos(float x,float y) 
        {
    
        }
        private void SetMaterial() 
        {
            //renderer.material.mainTexture
        }
    
        public class DeployData 
        {
            public float x;
            public float y;
            public float size;
        }
    
        public enum OpenType 
        {
            CHARATOR_VIEW,
            CHARATOR_PACK,
        }
    
    }
  • 相关阅读:
    ZOJ3209 Treasure Map —— Danc Links 精确覆盖
    HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题
    FZU1686 神龙的难题 —— Dancing Links 可重复覆盖
    POJ3074 Sudoku —— Dancing Links 精确覆盖
    HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
    HDU3533 Escape —— BFS / A*算法 + 预处理
    HDU3567 Eight II —— IDA*算法
    HDU1560 DNA sequence —— IDA*算法
    FZU2150 Fire Game —— BFS
    POJ3087 Shuffle'm Up —— 打表找规律 / map判重
  • 原文地址:https://www.cnblogs.com/kadaj/p/3780032.html
Copyright © 2011-2022 走看看