public IEnumerator RefreshRendererObject() { if (rendererObject == null) { yield break; } SceneSettings settings = FindObjectOfType(typeof(SceneSettings)) as SceneSettings; if (settings == null) { yield break; } HarmonyRenderer renderer = rendererObject.GetComponent <HarmonyRenderer>(); HarmonyAnimation animation = rendererObject.GetComponent <HarmonyAnimation>(); HarmonyAudio audio = rendererObject.GetComponent <HarmonyAudio>(); if ((renderer == null) || (animation == null) || (audio == null)) { yield break; } renderer.LoadClipIndex(settings.clipIdx); // Make sure sound is all downloaded before playing animation. yield return(StartCoroutine(audio.WaitForDownloads())); // Loop animation indefinitely. animation.ResetAnimation(); animation.LoopAnimation(frameRate, settings.clipIdx); animation.ResumeAnimation(); // resume if paused. }
IEnumerator Start() { SceneSettings settings = FindObjectOfType(typeof(SceneSettings)) as SceneSettings; if (settings != null) { if (settings.clipNames.Length > 0) { rendererObject = new GameObject("RendererObject"); rendererObject.transform.parent = settings.viewerGroup.transform; HarmonyRenderer renderer = rendererObject.AddComponent <HarmonyRenderer>(); HarmonyAnimation animation = rendererObject.AddComponent <HarmonyAnimation>(); HarmonyAudio audio = rendererObject.AddComponent <HarmonyAudio>(); AudioSource templateAudioSource = rendererObject.AddComponent <AudioSource>(); // Linear rolloff. Easier for sound to be heard. templateAudioSource.rolloffMode = AudioRolloffMode.Linear; // Set up clip collection in renderer. renderer.projectFolder = settings.projectFolder; renderer.clipNames = settings.clipNames; renderer.LoadClipIndex(settings.clipIdx); // Adjust renderer object size to fit in camera. Bounds box = renderer.CalculateCurrentBoundingBox(); float scaleFactor = 5.0f / Mathf.Max(box.size.x, box.size.y); rendererObject.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1.0f); // Make sure sound is all downloaded before playing animation. yield return(StartCoroutine(audio.WaitForDownloads())); // Loop animation indefinitely. animation.ResetAnimation(); animation.LoopAnimation(frameRate, settings.clipIdx); } } }
void CreateCharacterAt(Vector3 pos) { //Message.Log( "Create Character at: " + pos ); #if CLONE_ON_TOUCH GameObject rendererObject = GameObject.Instantiate(gameObjectToClone) as GameObject; rendererObject.transform.localPosition = new Vector3(pos.x, pos.y, pos.z); rendererObject.transform.parent = gameObject.transform; HarmonyRenderer renderer = rendererObject.GetComponent <HarmonyRenderer>(); HarmonyAnimation animation = rendererObject.GetComponent <HarmonyAnimation>(); #else GameObject rendererObject = new GameObject("Space Duck"); rendererObject.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f); rendererObject.transform.localPosition = new Vector3(pos.x, pos.y, pos.z); rendererObject.transform.parent = gameObject.transform; HarmonyRenderer renderer = rendererObject.AddComponent <HarmonyRenderer>(); HarmonyAnimation animation = rendererObject.AddComponent <HarmonyAnimation>(); Rigidbody2D rigidBody = rendererObject.AddComponent <Rigidbody2D>(); PolygonCollider2D collider = rendererObject.AddComponent <PolygonCollider2D>(); // Set up clip collection in renderer. renderer.projectFolder = liveProjectFolder; renderer.clipNames = XML_StageLoader.loadStageClipNames(renderer.projectFolder).ToArray(); renderer.syncCollider = HarmonyRenderer.SyncCollider.eAlways; renderer.colliderShape = colliderShape; #endif // CLONE_ON_TOUCH // Randomly play a clip. if (renderer.clipNames.Length > 0) { animation.LoopAnimation(24.0f, rand.Next() % renderer.clipNames.Length); } }
void Update() { HarmonyAudio audio = GetComponent <HarmonyAudio>(); if ((audio != null) && !audio.isReady) { return; } HarmonyAnimation animation = GetComponent <HarmonyAnimation>(); if (animation == null) { return; } HarmonyRenderer renderer = GetComponent <HarmonyRenderer>(); if ((renderer == null) || (renderer.clipNames == null)) { return; } //if ( !renderer.isReady ) // return; // Almost at end of our scheduled list, reschedule a new sequence. if (renderer.clipNames.Length > animation.scheduledCount) { // Play all clips foreach (string clipName in renderer.clipNames) { animation.PlayAnimation(frameRate, clipName); } } }
void postStart() { HarmonyRenderer renderer = GetComponent <HarmonyRenderer>(); // Preemptively load clip. renderer.LoadClipIndex(0 /* first clip */); HarmonyAnimation animation = gameObject.GetComponent <HarmonyAnimation>(); if (animation != null) { HarmonyAnimation.CallbackEvents callbacks = new HarmonyAnimation.CallbackEvents(); // Trigger a callback at frame 10 of animation. callbacks.AddCallbackAtFrame(10.0f, CallbackMethod1); // Trigget a callback at end of animation. callbacks.AddCallbackAtEnd(CallbackMethod2); // Loop animation indefinitely. animation.LoopAnimation(frameRate, 0 /* first clip */, 1.0f /*first frame*/, false /*forward*/, callbacks); } renderer.readyToRender = true; }
void OnGUI() { SceneSettings settings = FindObjectOfType(typeof(SceneSettings)) as SceneSettings; HarmonyAnimation animation = FindObjectOfType(typeof(HarmonyAnimation)) as HarmonyAnimation; HarmonyRendererController controller = FindObjectOfType(typeof(HarmonyRendererController)) as HarmonyRendererController; // Update font size according to dpi. if (!initialized) { int realFontSize = (Screen.dpi != 0) ? (int)(Screen.dpi * fontMult * fontSize) : fontSize; // Setup styles. labelStyle = new GUIStyle(GUI.skin.GetStyle("label")); labelStyle.fontSize = realFontSize; labelStyle.wordWrap = false; buttonStyle = new GUIStyle(GUI.skin.GetStyle("button")); buttonStyle.fontSize = realFontSize; buttonStyle.stretchWidth = false; initialized = true; } float width = Screen.width; float height = Screen.height; // Add button to go back to main browser scene. GUIContent buttonText = new GUIContent("Go Back"); Rect buttonRect = GUILayoutUtility.GetRect(buttonText, buttonStyle); int buttonWidth = (int)(buttonRect.width + 1); int buttonHeight = (int)(buttonRect.height + 1); // Lower right corner buttonRect = new Rect(width - buttonWidth - 5, height - buttonHeight - 5, buttonWidth, buttonHeight); if (GUI.Button(buttonRect, buttonText, buttonStyle)) { // Exit scene. Delete controller object and enable // browser group hierarchy. if (controller != null) { Destroy(controller.gameObject); } #if UNITY_WEBGL && !UNITY_EDITOR WebGL_FileSystem.RemovePreparedResource(settings.projectFolder); #endif settings.viewerGroup.SetActive(false); settings.browserGroup.SetActive(true); } // If more than 1 clip, add browser for previous/next clip. if (settings != null) { int nClips = settings.clipNames.Length; // Add button for previous clip. GUI.enabled = (settings.clipIdx >= 1); { buttonText = new GUIContent("Prev"); buttonRect = GUILayoutUtility.GetRect(buttonText, buttonStyle); buttonWidth = (int)(buttonRect.width + 1); buttonHeight = (int)(buttonRect.height + 1); // Lower middle side buttonRect = new Rect((width / 2) - buttonWidth - 5, height - ((buttonHeight + 5) * 2), buttonWidth, buttonHeight); if (GUI.Button(buttonRect, buttonText, buttonStyle)) { --settings.clipIdx; // Update clip index and restart animation. if (controller != null) { StartCoroutine(controller.RefreshRendererObject()); } } } // Add button for next clip. GUI.enabled = (settings.clipIdx < (nClips - 1)); { buttonText = new GUIContent("Next"); buttonRect = GUILayoutUtility.GetRect(buttonText, buttonStyle); buttonWidth = (int)(buttonRect.width + 1); buttonHeight = (int)(buttonRect.height + 1); // Lower middle side buttonRect = new Rect((width / 2) + 5, height - ((buttonHeight + 5) * 2), buttonWidth, buttonHeight); if (GUI.Button(buttonRect, buttonText, buttonStyle)) { ++settings.clipIdx; // Update clip index and restart animation. if (controller != null) { StartCoroutine(controller.RefreshRendererObject()); } } } GUI.enabled = true; } // Add button for play/pause animation. GUI.enabled = (animation != null); buttonText = new GUIContent(((animation == null) || animation.paused == true) ? "Play" : "Pause"); buttonRect = GUILayoutUtility.GetRect(buttonText, buttonStyle); buttonWidth = (int)(buttonRect.width + 1); buttonHeight = (int)(buttonRect.height + 1); // Lower middle side buttonRect = new Rect((width - buttonWidth) / 2, height - buttonHeight - 5, buttonWidth, buttonHeight); if (GUI.Button(buttonRect, buttonText, buttonStyle)) { if (animation != null) { // Pause or Resume animations. animation.PauseResumeAnimation(); // Change pause state of AudioListener as well. AudioListener.pause = animation.paused; } } GUI.enabled = true; // Add label for current clip. if (settings != null) { if (settings.clipIdx >= 0 && settings.clipIdx < settings.clipNames.Length) { GUIContent labelText = new GUIContent(settings.clipNames[settings.clipIdx]); Rect labelRect = GUILayoutUtility.GetRect(labelText, labelStyle); int labelWidth = (int)labelRect.width; int labelHeight = (int)labelRect.height; // Upper middle side. labelRect = new Rect((width - labelWidth) / 2, 5, labelWidth, labelHeight); GUI.Label(labelRect, labelText, labelStyle); } } }
private void Update() { if (clipNames == null) { return; } #if (UNITY_WEBGL) && !UNITY_EDITOR if (!readyToRender) { return; } #endif float frame = 1.0f; HarmonyAnimation animation = GetComponent <HarmonyAnimation>(); if (animation != null) { animation.Step(Time.deltaTime); frame = animation.frame; frame = (float)Math.Floor(frame); SetClipName(animation.clipName); } bool animationIsDirty = (lastFrame != frame); if ((clipIdx < 0) || (clipIdx >= clipNames.Length)) { return; } string clipName = clipNames[clipIdx]; uint intColor = renderingWithDirectX ? ((uint)(color.a * 255) << 24) + ((uint)(color.r * color.a * 255) << 16) + ((uint)(color.g * color.a * 255) << 8) + ((uint)(color.b * color.a * 255) << 0) : ((uint)(color.a * 255) << 24) + ((uint)(color.b * color.a * 255) << 16) + ((uint)(color.g * color.a * 255) << 8) + ((uint)(color.r * color.a * 255) << 0); if (scriptId == -1) { scriptId = Internal.CreateRenderScript(liveProjectFolder, clipName); } if (scriptId != -1) { bool scriptIsDirty = animationIsDirty || (lastColor != intColor); scriptIsDirty = scriptIsDirty || UpdateProps(scriptId); #if UNITY_EDITOR scriptIsDirty = scriptIsDirty || (lastDiscretizationStep != discretizationStep); lastDiscretizationStep = discretizationStep; #endif if (scriptIsDirty) { Internal.UpdateRenderScript(scriptId, liveProjectFolder, clipName, sheetResolution, frame, intColor, discretizationStep); } if (renderTarget == RenderTarget.eRenderTexture) { // renderable object attached to current game object. // Attach texture to it on which we will render. Renderer renderer = GetComponent <Renderer>(); if ((renderer != null) && (renderer.sharedMaterial)) { bool textureIsDirty = scriptIsDirty || (liveTextureViewport != renderTextureViewport); // No need to update texture at every frame if it did not change. if (textureIsDirty) { // Pass texture pointer to the plugin RenderTexture rendererTexture = renderer.sharedMaterial.mainTexture as RenderTexture; if ((rendererTexture == null) || (rendererTexture.width != renderTextureViewport.width) || (rendererTexture.height != renderTextureViewport.height)) { // Using RenderTexture instead of Texture2D as it resides in default memory pool in directx and allows // direct rendering to texture. rendererTexture = new RenderTexture((int)renderTextureViewport.width, (int)renderTextureViewport.height, 16, RenderTextureFormat.ARGB32); rendererTexture.filterMode = FilterMode.Bilinear; rendererTexture.Create(); #if UNITY_EDITOR // Clear buffer to avoid getting garbage if update is delayed in scene view. Graphics.SetRenderTarget(rendererTexture); GL.Clear(true, true, Color.clear); Graphics.SetRenderTarget(null); #endif // Set texture onto our material if (!Application.isPlaying) { Material materialCopy = Material.Instantiate(renderer.sharedMaterial) as Material; materialCopy.name = renderer.sharedMaterial.name; materialCopy.mainTexture = rendererTexture; UnityEngine.Object.DestroyImmediate(renderer.sharedMaterial.mainTexture); UnityEngine.Object.DestroyImmediate(renderer.sharedMaterial); renderer.material = materialCopy; } else { UnityEngine.Object.Destroy(renderer.sharedMaterial.mainTexture); UnityEngine.Object.Destroy(renderer.sharedMaterial); renderer.material.mainTexture = rendererTexture; } rendererTexturePtr = IntPtr.Zero; } // Retrieve Texture Id/Ptr if not set already. This is a costly operation so only do it once. if (rendererTexturePtr == IntPtr.Zero) { rendererTexturePtr = rendererTexture.GetNativeTexturePtr(); } // Pass texture pointer to the plugin Internal.CreateRenderTextureEvent(scriptId, renderTextureViewport.x, renderTextureViewport.y, renderTextureViewport.width, renderTextureViewport.height, renderTextureScale, rendererTexturePtr); liveTextureViewport = renderTextureViewport; } } else { // Invalidate texture viewport... liveTextureViewport = new Rect(0, 0, 0, 0); } } else { // Proceed with direct render in LateUpdate so that // all matrices are computed when render request is processed. scriptIdToRender = scriptId; } } // Update attached components and child game objects. // Only performed when application is playing to avoid modifying game object // parameters and triggering scene changes. if (Application.isPlaying && animationIsDirty) { bool doSync = (syncCollider == SyncCollider.eAlways) || ((syncCollider == SyncCollider.eOnce) && !isColliderSynced); if (doSync) { ColliderKey key; key.clipName = clipName; key.frame = frame; key.colliderShape = colliderShape; if (currentCollider != null) { currentCollider.enabled = false; } int colliderIndex = colliderKeys.IndexOf(key.ToString()); if (colliderIndex != -1) { Collider2D collider = colliderLinks[colliderIndex]; collider.enabled = true; collider.isTrigger = colliderTrigger; currentCollider = collider; } else { Collider2D collider = CreateCollider(scriptId, key); collider.enabled = true; collider.isTrigger = colliderTrigger; currentCollider = collider; } isColliderSynced = true; } } // Calculate bounding box for gizmos rendering. // Only used in editor. #if UNITY_EDITOR if ((scriptId != -1) && (renderTarget == RenderTarget.eScreen)) { Internal.CalculateBoundingBox(scriptId, interactiveBox); } #endif lastFrame = frame; lastColor = intColor; }