void ApplyEffects(WorldObjectBehaviour worldObj, ResourceActivationContext effectContext, WorldObjectEffectPlayer resource) { if (resource.Effects != null) { foreach (var eref in resource.Effects) { IWorldObjectEffectHandler handler; if (m_effectHandlers.TryGetValue(eref.Type, out handler)) { var appliedEffect = new AppliedEffect(handler, worldObj, effectContext, resource, eref); handler.ApplyEffect(appliedEffect); m_appliedEffects.Add(effectContext.InstanceId, appliedEffect); } else { var errStr = string.Format("Could not find handler for effect type {0}", eref.Type); SystemErrorHandler.Instance.ReportError(errStr); m_logger.Warning(errStr); } } } }
public virtual void RemoveEffect(AppliedEffect appliedEffect) { var effects = m_appliedEffects[appliedEffect.WorldObject.ActivationContext.InstanceId]; if (effects != null && effects.Count > 0) { var last = effects.Last(); effects.RemoveAll(a => a.ActivationContext.InstanceId == appliedEffect.ActivationContext.InstanceId); if (last.ActivationContext.InstanceId == appliedEffect.ActivationContext.InstanceId) { // The last animation is the one that's currently animating. // Stop it. StopEffect(appliedEffect, (T)appliedEffect.Effect); if (effects.Count > 0) { last = effects.Last(); // Start playing the next-last one. StartEffect(last, (T)last.Effect); } } } }
private void StartAnimation(AppliedEffect appliedEffect) { var animName = ((GenericScriptObject)appliedEffect.Effect)["name"].ToString(); var animTarget = appliedEffect.WorldObject.GetAnimationTarget(); StartAnimationWithName(appliedEffect, animTarget, animName); }
public virtual void ApplyEffect(AppliedEffect appliedEffect) { if (appliedEffect.Effect is T) { m_appliedEffects.Add(appliedEffect.WorldObject.ActivationContext.InstanceId, appliedEffect); StartEffect(appliedEffect, (T)appliedEffect.Effect); } }
protected override void StopEffect(AppliedEffect appliedEffect, GenericScriptObject effect) { var objAnim = appliedEffect.WorldObject.GetAnimationTarget().GetComponent <Animation>(); if (objAnim) { objAnim.Stop(); } }
public void RemoveEffect(AppliedEffect appliedEffect) { MonoBehaviour effect; if (m_effects.TryGetValue(appliedEffect, out effect)) { GameObject.Destroy(effect); } }
public void RemoveEffect(AppliedEffect appliedEffect) { var effect = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.gameObject; var particle = effect.GetComponent <ParticleSystem>(); if (particle != null) { GameObject.Destroy(particle); } }
public override void RemoveEffect(AppliedEffect appliedEffect) { var objAnim = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.GetComponent <Animation>(); if (objAnim) { objAnim.RemoveClip(appliedEffect.Effect.Id); } base.RemoveEffect(appliedEffect); }
protected void StartAnimationWithName(AppliedEffect appliedEffect, GameObject animTarget, string name) { var anim = animTarget.GetComponent <Animation>(); if (anim && name != null) { foreach (var a in anim) { var state = (AnimationState)a; if (state.name.Equals(name, StringComparison.OrdinalIgnoreCase)) { state.speed = (float)appliedEffect.EffectPlayer.Speed.GetValueOrDefault(1); anim.clip = state.clip; anim.Play(); break; } } } }
public void ApplyEffect(AppliedEffect appliedEffect) { var anim = appliedEffect.Effect as UnityAnimation; if (anim != null) { var asset = anim.Asset; if (asset != null) { var animTgt = appliedEffect.WorldObject.GetAnimationTarget(); if (animTgt) { // Target may have been destroyed somewhere var objAnim = animTgt.AddComponent <WorldObjectAnimation>(); objAnim.AnimationAsset = asset; m_effects.Add(appliedEffect, objAnim); } } } }
public override void ApplyEffect(AppliedEffect appliedEffect) { //Retrieve the container holding the object and add animation component to it var sceneObject = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.gameObject; var sAnimations = appliedEffect.Effect as ScriptedAnimation; if (sceneObject && sAnimations != null) { if (sceneObject.GetComponent <Animation>() == null) { sceneObject.AddComponent <Animation>(); } Transform transform = sceneObject.transform; //Build a new animation clip, storing the keyframes in a 2D array AnimationClip clip = new AnimationClip { legacy = true }; float time = 0.0f; ListDictionary <string, PropertyKeyframe> propertyKeyframes = new ListDictionary <string, PropertyKeyframe>(); Action <string, float, string, double?> addKeyframe = (prop, _time, easing, val) => { if (val.HasValue) { propertyKeyframes.Add(prop, new PropertyKeyframe { Easing = easing, Keyframe = new Keyframe(_time, (float)val.Value) }); } }; Action <string, float, string, NullableVector> addKeyframeVector = (prop, _time, easing, vec) => { if (vec != null) { addKeyframe(prop + ".x", _time, easing, vec.X); addKeyframe(prop + ".y", _time, easing, vec.Y); addKeyframe(prop + ".z", _time, easing, vec.Z); } }; // Let's add initial settings for the animation //addKeyframeVector("localPosition", 0, foreach (var kFrame in sAnimations.Keyframes) { time += (float)kFrame.Duration.TotalSeconds; if (kFrame.Properties != null) { foreach (var property in kFrame.Properties) { if (property is MotiveTransform) { var xform = property as MotiveTransform; addKeyframeVector("localPosition", time, kFrame.Easing, xform.Position); addKeyframeVector("localScale", time, kFrame.Easing, xform.Scale); if (xform.Rotation != null) { var rot = Quaternion.Euler( (float)xform.Rotation.X.GetValueOrDefault(), (float)xform.Rotation.Y.GetValueOrDefault(), (float)xform.Rotation.Z.GetValueOrDefault()); addKeyframe("localRotation.x", time, kFrame.Easing, rot.x); addKeyframe("localRotation.y", time, kFrame.Easing, rot.y); addKeyframe("localRotation.z", time, kFrame.Easing, rot.z); addKeyframe("localRotation.w", time, kFrame.Easing, rot.w); } } else { m_logger.Warning("Unknown property type in keyframe: {0}", property.Type); } } } } //Set Easing Properties //SetEasing(sAnimations); //Setting the animation curves to the animation clip foreach (var kv in propertyKeyframes) { SetEasing(kv.Value); // NOTE: For now we know that all properties are transforms. We can extend // the model to handle different property types in the future. clip.SetCurve("", typeof(Transform), kv.Key, new AnimationCurve(kv.Value.Select(v => v.Keyframe).ToArray())); //clip.SetCurve("", typeof(Transform), kv.Key, AnimationCurve.Linear(0, 0, 1, 1)); } //Loop Check if (sAnimations.Loop) { clip.wrapMode = WrapMode.Loop; } var anim = sceneObject.GetComponent <Animation>(); if (anim) { anim.AddClip(clip, sAnimations.Id); } // Uncomment this in the editor to stash a version of the created animation //UnityEditor.AssetDatabase.CreateAsset(clip, "Assets/" + sAnimations.Id + ".anim"); base.ApplyEffect(appliedEffect); } }
protected override void StartEffect(AppliedEffect appliedEffect, ScriptedAnimation effect) { var sceneObject = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.gameObject; StartAnimationWithName(appliedEffect, sceneObject, effect.Id); }
protected override void StopEffect(AppliedEffect appliedEffect, ScriptedAnimation effect) { var sceneObject = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.gameObject; base.StopAnimation(appliedEffect, sceneObject); }
public void ApplyEffect(AppliedEffect appliedEffect) { var anims = appliedEffect.Effect as ScriptedParticleEffect; var animTarget = appliedEffect.WorldObject.GetAnimationTarget().transform.parent.gameObject; var particles = animTarget.AddComponent <ParticleSystem>(); //Target two materials: mat for default and userMat for rewriting Material mat = Resources.Load("ParticleGlow") as Material; Material userMat = Resources.Load("UserParticleMaterial") as Material; //Set default particle emission shape var sh = particles.shape; sh.shapeType = ParticleSystemShapeType.Sphere; sh.radius = kDefaultRadius; //Apply color, max particles, particle size and looping ParticleSystem.MainModule main = particles.main; Color color; main.startSpeed = kDefaultStartSpeed; main.startLifetime = kDefaultStartLifetime; main.startSize = kDefaultStartSize; main.simulationSpeed = (float)appliedEffect.EffectPlayer.Speed.GetValueOrDefault(1); if (anims.Color == null) { color = Color.white; } else { color = new Color32((byte)anims.Color.R, (byte)anims.Color.G, (byte)anims.Color.B, (byte)(anims.Color.A * 255)); } main.startColor = color; if (anims.MaxParticles.HasValue) { main.maxParticles = anims.MaxParticles.Value; } if (anims.ParticleSize.HasValue) { main.startSizeMultiplier = anims.ParticleSize.Value; } main.loop = anims.Loop; //Rate over time ParticleSystem.EmissionModule emit = particles.emission; emit.rateOverTime = kDefaultEmissionRate; if (anims.RateOverTime.HasValue) { emit.rateOverTime = anims.RateOverTime.Value; } //Load material image if (anims.ImageUrl == null) { particles.GetComponent <ParticleSystemRenderer>().material = mat; } else { ImageLoader.LoadTexture(anims.ImageUrl, (tex) => { if (tex) { userMat.mainTexture = tex; particles.GetComponent <ParticleSystemRenderer>().material = userMat; } }); } }
protected override void StartEffect(AppliedEffect appliedEffect, GenericScriptObject effect) { StartAnimation(appliedEffect); }
protected void StopAnimation(AppliedEffect appliedEffect, GameObject animTarget) { var anim = animTarget.GetComponent <Animation>(); anim.Stop(); }
protected abstract void StartEffect(AppliedEffect appliedEffect, T effect);