// Use this for initialization protected override void OnInit() { if (transform != null) { mDefaultPos = transform.localPosition; } if (mSpinningRender != null) { mSpinningRender.gameObject.SetActive(false); } if (mDefaultRender != null) { mDefaultRender.SetActive(true); } if (DynamicAtlasManager.IsAvailable(mDynamicAtlasType) && !string.IsNullOrEmpty(spriteName)) { TryRegisterDACallbacks(); TryLoadDynamicSprite(); } if (mSpriteName != null) { if (GameEngine.Instance != null) { GameEngine.Instance.LTStaticAtlas.LoadStaticAtlas(mSpriteName, succ => { SetStaticSpriteAtlas(); }); } } base.OnInit(); }
static DynamicAtlasManager() { GameObject gameObject = new GameObject("DynamicAtlasParent"); DontDestroyOnLoad(gameObject); DynamicAtlasManager.Instance = gameObject.AddComponent <DynamicAtlasManager>(); }
void TryRegisterUVCallback() { if (!m_UVCallbackAdded) { DynamicAtlasManager.GetInstance().AddSpriteUVUpdateCallback(m_DynamicAtlasType, MarkAsChanged); } }
void UpdateUV() { if (AtlasType == FXAtlasType.Static) { if (m_Atlas != null) { Texture texture = m_Atlas.spriteMaterial.mainTexture; if (texture != null) { m_SpriteUV.Set(m_Sprite.x, m_Sprite.y, m_Sprite.width, m_Sprite.height); m_SpriteUV = NGUIMath.ConvertToTexCoords(m_SpriteUV, texture.width, texture.height); } } } else if (AtlasType == FXAtlasType.Dynamic) { DynamicAtlasManager dynamic_atlas_manager = DynamicAtlasManager.GetInstance(); if (dynamic_atlas_manager != null) { DynamicAtlas the_atlas = dynamic_atlas_manager.GetDynamicAtlas(m_DynamicAtlasType); if (the_atlas != null) { Texture texture = the_atlas.m_Material.mainTexture; if (texture != null && m_Sprite != null) { m_SpriteUV.Set(m_Sprite.x, m_Sprite.y, m_Sprite.width, m_Sprite.height); m_SpriteUV = NGUIMath.ConvertToTexCoords(m_SpriteUV, texture.width, texture.height); } } } } }
void OnTextureAsyncFailed() { if (isLoadHeroHead) { return; } if (spriteName.Equals(DynamicAtlas.CurrentAsyncedTexture)) { if (mSpinningRender != null) { mSpinningRender.gameObject.SetActive(false); } } DynamicSpriteData sprite_data = DynamicAtlasManager.GetInstance().GetAtlasSprite(spriteName, mDynamicAtlasType) as DynamicSpriteData; if (sprite_data == null || sprite_data.TextureReference == null) { if (mDefaultRender != null) { mDefaultRender.SetActive(true); } } }
/// <summary> /// Retrieve the atlas sprite referenced by the spriteName field. /// </summary> public UISpriteData GetAtlasSprite() { if (isLoadHeroHead) { return(null); } if (!mSpriteSet) { mSprite = null; } if (mSprite == null) { if (!string.IsNullOrEmpty(mSpriteName)) { UISpriteData sp = null; // get sprite data from dynamic atlas if (DynamicAtlasManager.IsAvailable(mDynamicAtlasType)) { sp = DynamicAtlasManager.GetInstance().GetAtlasSprite(mSpriteName, mDynamicAtlasType); } if (sp == null) { return(null); } SetAtlasSprite(sp); } } return(mSprite); }
public void MarkAsChanged() { m_Changed = true; if (DynamicAtlasManager.IsAvailable(m_DynamicAtlasType)) { m_Sprite = DynamicAtlasManager.GetInstance().GetAtlasSprite(m_SpriteName, m_DynamicAtlasType); } }
void UnregisterUVCallback() { if (m_UVCallbackAdded) { DynamicAtlasManager.GetInstance().RemoveSpriteUVUpdateCallback(m_DynamicAtlasType, MarkAsChanged); m_UVCallbackAdded = false; } }
void UnloadDynamicSprite() { if (m_DynamicSpriteLoaded) { DynamicAtlasManager.GetInstance().UnloadDynamicSprite(m_SpriteName, m_DynamicAtlasType); m_DynamicSpriteLoaded = false; } }
void Awake() { m_instance = this; if (Application.isPlaying) { DontDestroyOnLoad(gameObject); } }
void TryLoadDynamicSprite() { if (!m_DynamicSpriteLoaded) { if (!string.IsNullOrEmpty(m_LastSpriteName)) { DynamicAtlasManager.GetInstance().UnloadDynamicSprite(m_LastSpriteName, m_DynamicAtlasType); } DynamicAtlasManager.GetInstance().LoadDynamicSprite(m_SpriteName, m_DynamicAtlasType); m_DynamicSpriteLoaded = true; } }
void OnDisable() { if (AtlasType == FXAtlasType.Dynamic) { if (DynamicAtlasManager.IsAvailable(m_DynamicAtlasType)) { UnloadDynamicSprite(); UnregisterUVCallback(); } } //RemoveParticleObjects(); }
void ShutDown() { EnableRenderer(false); if (AtlasType == FXAtlasType.Dynamic) { if (DynamicAtlasManager.IsAvailable(m_DynamicAtlasType)) { UnloadDynamicSprite(); UnregisterUVCallback(); } } }
void TryRegisterDACallbacks() { if (isLoadHeroHead) { return; } if (!mDACallbackFlag) { DynamicAtlasManager.GetInstance().AddSpriteUVUpdateCallback(mDynamicAtlasType, OnUVUpdated); DynamicAtlasManager.GetInstance().AddTextureAsyncCallback(mDynamicAtlasType, OnTextureAsyncSucceeded, OnTextureAsyncFailed); mDACallbackFlag = true; } }
void UnloadDynamicSprite() { if (isLoadHeroHead) { return; } if (mLoadFlag) { DynamicAtlasManager.GetInstance().UnloadDynamicSprite(mSpriteName, mDynamicAtlasType); mLoadFlag = false; mSprite = null; } }
void UnregisterDACallbacks() { if (isLoadHeroHead) { return; } if (mDACallbackFlag) { DynamicAtlasManager.GetInstance().RemoveSpriteUVUpdateCallback(mDynamicAtlasType, OnUVUpdated); DynamicAtlasManager.GetInstance().RemoveTextureAsyncCallback(mDynamicAtlasType, OnTextureAsyncSucceeded, OnTextureAsyncFailed); mDACallbackFlag = false; } }
//use this for disable notification protected override void OnDisable() { if (isLoadHeroHead) { return; } base.OnDisable(); if (DynamicAtlasManager.IsAvailable(mDynamicAtlasType) && !string.IsNullOrEmpty(spriteName)) { UnloadDynamicSprite(); UnregisterDACallbacks(); } }
void Update() { if (AtlasType == FXAtlasType.Dynamic) { if (DynamicAtlasManager.IsAvailable(m_DynamicAtlasType)) { TryRegisterUVCallback(); TryLoadDynamicSprite(); } } if (m_Changed) { UpdateSprite(); m_Changed = false; } #if UNITY_EDITOR UpdateGeometry(); #endif }
public void TryLoadDynamicSprite() { if (isLoadHeroHead || GameEngine.Instance.LTStaticAtlas.IsHeadAtlas(spriteName)) { return; } if (!mLoadFlag) { if (!string.IsNullOrEmpty(spriteName)) { if (mSpinningRender != null) { mSpinningRender.SetActive(true); } if (mDefaultRender != null) { mDefaultRender.SetActive(true); } UISpriteData sp = DynamicAtlasManager.GetInstance().GetAtlasSprite(spriteName, mDynamicAtlasType); if (sp != null) { // already loaded if (mSpinningRender != null) { mSpinningRender.SetActive(false); } if (mDefaultRender != null) { mDefaultRender.SetActive(false); } } // reload, add reference DynamicAtlasManager.GetInstance().LoadDynamicSprite(mSpriteName, mDynamicAtlasType); } mLoadFlag = true; } }
// Update is called once per frame protected override void OnUpdate() { base.OnUpdate(); if (DynamicAtlasManager.IsAvailable(mDynamicAtlasType) && !string.IsNullOrEmpty(spriteName)) { TryRegisterDACallbacks(); TryLoadDynamicSprite(); } if (mChanged || !mSpriteSet) { mSpriteSet = true; mChanged = true; if (mSprite != null) { mSprite = ClipAtlasSprite(mOriginSprite); } } }
void Update() { m_CurrentTime += Time.deltaTime; if (m_CurrentTime >= Duration) { m_CurrentTime = 0; } if (AtlasType == FXAtlasType.Dynamic) { if (DynamicAtlasManager.IsAvailable(m_DynamicAtlasType)) { TryRegisterUVCallback(); TryLoadDynamicSprite(); } } if (m_Changed) { UpdateSprite(); } }
void UpdateGeometry() { #if UNITY_EDITOR if (mUpdateFrame != Time.frameCount || !Application.isPlaying) #else if (mUpdateFrame != Time.frameCount) #endif { mUpdateFrame = Time.frameCount; if (GetComponent <ParticleSystem>() != null) { // int max_particles = GetComponent <ParticleSystem>().maxParticles; m_Particles.Adjust(max_particles); bool isBufferChanged = m_Meshes.Adjust(max_particles, true); ParticleSystem.Particle[] particles = m_Particles.GetBuffer(); GetComponent <ParticleSystem>().GetParticles(particles); // ParticleMesh[] meshes = m_Meshes.GetBuffer(); int mesh_count = meshes.Length; int particle_count = GetComponent <ParticleSystem>().particleCount; if (isBufferChanged) { UnityEngine.Profiling.Profiler.BeginSample("FXBillboardParticle 001"); for (int i = particle_count; i < mesh_count; i++) { if (meshes[i].m_Renderer != null) { meshes[i].m_Renderer.enabled = false; } } UnityEngine.Profiling.Profiler.EndSample(); } UnityEngine.Profiling.Profiler.BeginSample("FXBillboardParticle 002"); for (int i = 0; i < particle_count; i++) { ParticleSystem.Particle particle = particles[i]; float factor = (particle.startLifetime - particle.remainingLifetime) / particle.startLifetime; float size = particle.GetCurrentSize(GetComponent <ParticleSystem>()) * Mathf.Lerp(FromSize, ToSize, factor); Matrix4x4 transform_matrix = Matrix4x4.TRS(particle.position, Quaternion.AngleAxis(particle.rotation, -Camera.main.transform.forward), new Vector3(size, size, size)); meshes[i].PreUpdate(transform); if (AtlasType == FXAtlasType.Static) { if (meshes[i].m_Renderer != null && m_Atlas != null) { meshes[i].m_Renderer.sharedMaterial = m_Atlas.spriteMaterial; meshes[i].m_Renderer.enabled = true; } } else if (AtlasType == FXAtlasType.Dynamic) { DynamicAtlasManager dynamic_atlas_manager = DynamicAtlasManager.GetInstance(); if (meshes[i].m_Renderer != null && dynamic_atlas_manager != null) { DynamicAtlas dynamic_atlas = dynamic_atlas_manager.GetDynamicAtlas(m_DynamicAtlasType); if (dynamic_atlas != null) { meshes[i].m_Renderer.sharedMaterial = dynamic_atlas.m_Material; meshes[i].m_Renderer.enabled = true; } } } if (GetComponent <ParticleSystem>().simulationSpace == ParticleSystemSimulationSpace.Local) { meshes[i].m_Vertices[0].Set(-0.5f, -0.5f, 0); meshes[i].m_Vertices[0] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[0]); meshes[i].m_Vertices[1].Set(-0.5f, 0.5f, 0); meshes[i].m_Vertices[1] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[1]); meshes[i].m_Vertices[2].Set(0.5f, 0.5f, 0); meshes[i].m_Vertices[2] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[2]); meshes[i].m_Vertices[3].Set(0.5f, -0.5f, 0); meshes[i].m_Vertices[3] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[3]); } else { Matrix4x4 world2local_matrix = transform.worldToLocalMatrix; meshes[i].m_Vertices[0].Set(-0.5f, -0.5f, 0); meshes[i].m_Vertices[0] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[0]); meshes[i].m_Vertices[0] = world2local_matrix.MultiplyPoint(meshes[i].m_Vertices[0]); meshes[i].m_Vertices[1].Set(-0.5f, 0.5f, 0); meshes[i].m_Vertices[1] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[1]); meshes[i].m_Vertices[1] = world2local_matrix.MultiplyPoint(meshes[i].m_Vertices[1]); meshes[i].m_Vertices[2].Set(0.5f, 0.5f, 0); meshes[i].m_Vertices[2] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[2]); meshes[i].m_Vertices[2] = world2local_matrix.MultiplyPoint(meshes[i].m_Vertices[2]); meshes[i].m_Vertices[3].Set(0.5f, -0.5f, 0); meshes[i].m_Vertices[3] = transform_matrix.MultiplyPoint(meshes[i].m_Vertices[3]); meshes[i].m_Vertices[3] = world2local_matrix.MultiplyPoint(meshes[i].m_Vertices[3]); } // Rect uv = m_SpriteUV; float frame = TileX * TileY * factor; int frame_index = (int)frame; int frameX = frame_index % TileX; int frameY = TileY - frame_index / TileY - 1; float stepX = m_SpriteUV.width / (float)TileX; float stepY = m_SpriteUV.height / (float)TileY; uv.Set(stepX * frameX + m_SpriteUV.x, stepY * frameY + m_SpriteUV.y, stepX, stepY); meshes[i].m_UVs[0].Set(uv.xMin, uv.yMin); meshes[i].m_UVs[1].Set(uv.xMin, uv.yMax); meshes[i].m_UVs[2].Set(uv.xMax, uv.yMax); meshes[i].m_UVs[3].Set(uv.xMax, uv.yMin); // Color color = particle.GetCurrentColor(GetComponent <ParticleSystem>()) * Color.Lerp(FromColor, ToColor, factor); meshes[i].m_Colors[0] = color; meshes[i].m_Colors[1] = color; meshes[i].m_Colors[2] = color; meshes[i].m_Colors[3] = color; meshes[i].m_Indices[0] = 0; meshes[i].m_Indices[1] = 1; meshes[i].m_Indices[2] = 2; meshes[i].m_Indices[3] = 0; meshes[i].m_Indices[4] = 2; meshes[i].m_Indices[5] = 3; meshes[i].Update(); } UnityEngine.Profiling.Profiler.EndSample(); } } }
void OnDestroy() { m_instance = null; }