using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class UIRoleSceneReplace : MonoBehaviour
{
    public string _ResPath;
    public bool _AutoFitSize = false;

    private void Update()
    {
        if (!_IsHideScene)
        {
            HideSceneObj();
        }

        if (!_ReplaceSkyBox)
        {
            ReplaceSkyBox();
        }
    }

    private bool _IsHideScene = false;
    private bool HideSceneObj()
    {
        GameObject sceneObj = GameObject.Find("sence");
        if (sceneObj == null)
        {
            _IsHideScene = false;
            return false;
        }

        sceneObj.SetActive(false);

        GameObject effObj1 = GameObject.Find("TX_cj_yun");
        if (effObj1 != null)
        {
            effObj1.SetActive(false);
        }

        GameObject effObj2 = GameObject.Find("dw_tuzi");
        if (effObj2 != null)
        {
            effObj2.SetActive(false);
        }

        GameObject effObj3 = GameObject.Find("B_skin");
        if (effObj3 != null)
        {
            effObj3.SetActive(false);
        }

        GameObject effObj4 = GameObject.Find("TX_changjing_lights (1)");
        if (effObj4 != null)
        {
            effObj4.SetActive(false);
        }

        GameObject effObj5 = GameObject.Find("TX_cj_xingx");
        if (effObj5 != null)
        {
            effObj5.SetActive(false);
        }

        GameObject effObj6 = GameObject.Find("TX_caihong");
        if (effObj6 != null)
        {
            effObj6.SetActive(false);
        }

        GameObject effObj7 = GameObject.Find("DL_suolian");
        if (effObj7 != null)
        {
            effObj7.SetActive(false);
        }

        GameObject effObj8 = GameObject.Find("DL_suolian (1)");
        if (effObj8 != null)
        {
            effObj8.SetActive(false);
        }

        GameObject effObj9 = GameObject.Find("DL_suolian (2)");
        if (effObj9 != null)
        {
            effObj9.SetActive(false);
        }

        GameObject effObj10 = GameObject.Find("DL_suolian01");
        if (effObj10 != null)
        {
            effObj10.SetActive(false);
        }

        _IsHideScene = true;
        return true;
    }

    private bool _ReplaceSkyBox = false;
    private void ReplaceSkyBox()
    {
        GameObject skyBox = GameObject.Find("SM_skybox01");
        if (skyBox != null)
        {
            _ReplaceSkyBox = true;
            ChangeRes(skyBox, _ResPath);
        }
    }

    protected void ChangeRes(GameObject skyBoxGO, string imgPath)
    {
        skyBoxGO.SetActive(true);
        var rawImage = skyBoxGO.GetComponent<MeshRenderer>();
        if (rawImage != null)
        {
            StartCoroutine( SetTexture(rawImage, imgPath));
        }
    }

    private IEnumerator SetTexture(MeshRenderer meshrender, string path)
    {
        string filePath = "";
#if UNITY_IPHONE
        filePath = "file://" + Application.streamingAssetsPath + "/" + path;
#else
        filePath = Application.streamingAssetsPath + "/" + path;
#endif
        WWW www = new WWW(filePath);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError("LoadImageError:" + www.error.ToString() + "," + path);
        }
        else
        {
            meshrender.sharedMaterial.SetTexture("_MainTex", www.texture);
        }
    }
    
}