unity 序列帧播放

    [SerializeField]
    private Image m_ScreenImage;             //序列帧播放的image
    [SerializeField]
    private int m_FrameRate = 30;                  // 一秒播放的帧数
    [SerializeField]
    private string m_strFolder= "";       // 序列帧通过Resource.load的加载,所以序列帧文件要放在Resource目录下,此变量为Resource下序列帧的存放目录
    [SerializeField]
    private string m_strPreName= "";    //每一序列帧文件名字相同部分,如第四十帧文件全名为9_Bumper_00040,则此处应为9_Bumper_000
    [SerializeField]
    private int m_nFromNo=0;  //开始播放帧
    [SerializeField]
    private int m_nEndNo=244;//结束帧
    [SerializeField]
    private int m_nFixLen = -1; //序列帧名字补全位数,-1为不补全,如第四十帧文件全名为9_Bumper_00040,m_strPreName为9_Bumper_000,则补全位数为2

    public void StartPlay(string id)
    {
        StartCoroutine(Play());
    }

   private IEnumerator Play()
    {int nCurrentTextureIndex = m_nFromNo;
        while (true)
        {
            // So long as the textures should be playing...
            Sprite t;
            //
            string strImagePath = "";
            string strNum = "";
            if (m_nFixLen == -1)
            {
                strNum = nCurrentTextureIndex.ToString();
            }
            else
            {
                string strFormat = "{0:";
                for (int i = 0; i < m_nFixLen; ++i)
                {
                    strFormat += "0";
                }
                strFormat += "}";
                strNum = string.Format(strFormat, nCurrentTextureIndex);
            }

            strImagePath = m_strFolder + "/" + m_strPreName + strNum;
            t = Resources.Load<Sprite>(strImagePath);
            m_ScreenImage.sprite = t;// Then increment the texture index (looping once it reaches the length of the textures array.
            //nCurrentTextureIndex = m_nFromNo + (nCurrentTextureIndex - m_nFromNo + 1) % (m_nEndNo - m_nFromNo + 1);//m_AnimTextures.Length;
            nCurrentTextureIndex++;
// Wait for the next frame.
            yield return m_FrameRateWait;
        }
    }

    void Start()
    {
        m_FrameRateWait = new WaitForSeconds(1f / m_FrameRate);
    }

 

posted @ 2018-01-10 14:32  81192  阅读(1222)  评论(0编辑  收藏  举报