public static Sprite[] GetSpriteFromDraggedPathsOrObjects() { List <Sprite> spriteList = new List <Sprite>(); foreach (UnityEngine.Object objectReference in DragAndDrop.objectReferences) { if (AssetDatabase.Contains(objectReference)) { if (objectReference is Sprite) { spriteList.Add(objectReference as Sprite); } else if (objectReference is Texture2D) { spriteList.AddRange((IEnumerable <Sprite>)SpriteUtility.TextureToSprites(objectReference as Texture2D)); } } } if (spriteList.Count > 0) { return(spriteList.ToArray()); } Sprite sprite = SpriteUtility.HandleExternalDrag(Event.current.type == EventType.DragPerform); if (!((UnityEngine.Object)sprite != (UnityEngine.Object)null)) { return((Sprite[])null); } return(new Sprite[1] { sprite }); }
private static bool CreateAnimation(GameObject gameObject, UnityEngine.Object[] frames) { Array.Sort <UnityEngine.Object>(frames, (UnityEngine.Object a, UnityEngine.Object b) => EditorUtility.NaturalCompare(a.name, b.name)); string message = string.Format("Create a new animation for the game object '{0}':", gameObject.name); string directoryName = Path.GetDirectoryName(AssetDatabase.GetAssetPath(frames[0])); string text = EditorUtility.SaveFilePanelInProject("Create New Animation", "New Animation", "anim", message, directoryName); if (string.IsNullOrEmpty(text)) { return(false); } AnimationClip animationClip = AnimationSelection.AllocateAndSetupClip(true); AssetDatabase.CreateAsset(animationClip, text); AnimationSelection.AddClipToAnimatorComponent(gameObject, animationClip); animationClip.frameRate = 12f; ObjectReferenceKeyframe[] array = new ObjectReferenceKeyframe[frames.Length]; for (int i = 0; i < array.Length; i++) { array[i] = default(ObjectReferenceKeyframe); array[i].value = SpriteUtility.RemapObjectToSprite(frames[i]); array[i].time = (float)i / animationClip.frameRate; } EditorCurveBinding binding = EditorCurveBinding.PPtrCurve(string.Empty, typeof(SpriteRenderer), "m_Sprite"); AnimationUtility.SetObjectReferenceCurve(animationClip, binding, array); return(true); }
private static void HandleExternalDrag(string[] paths, bool perform, ref List <Sprite> result) { for (int i = 0; i < paths.Length; i++) { string text = paths[i]; if (SpriteUtility.ValidPathForTextureAsset(text)) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (perform) { string text2 = AssetDatabase.GenerateUniqueAssetPath(Path.Combine("Assets", FileUtil.GetLastPathNameComponent(text))); if (text2.Length > 0) { FileUtil.CopyFileOrDirectory(text, text2); SpriteUtility.ForcedImportFor(text2); Sprite sprite = SpriteUtility.GenerateDefaultSprite(AssetDatabase.LoadMainAssetAtPath(text2) as UnityEngine.Texture2D); if (sprite != null) { result.Add(sprite); } } } } } }
public static bool HandleSingleSpriteDragIntoHierarchy(IHierarchyProperty property, Sprite sprite, bool perform) { GameObject gameObject = null; if (property != null && property.pptrValue != null) { UnityEngine.Object pptrValue = property.pptrValue; gameObject = (pptrValue as GameObject); } if (perform) { Vector3 defaultInstantiatePosition = SpriteUtility.GetDefaultInstantiatePosition(); GameObject gameObject2 = SpriteUtility.DropSpriteToSceneToCreateGO(sprite, defaultInstantiatePosition); if (gameObject != null) { Analytics.Event("Sprite Drag and Drop", "Drop single sprite to existing gameobject", "null", 1); gameObject2.transform.parent = gameObject.transform; gameObject2.transform.localPosition = Vector3.zero; } else { Analytics.Event("Sprite Drag and Drop", "Drop single sprite to empty hierarchy", "null", 1); } Selection.activeGameObject = gameObject2; } return(true); }
private static Sprite GenerateDefaultSprite(UnityEngine.Texture2D texture) { string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; Sprite result; if (textureImporter == null) { result = null; } else if (textureImporter.textureType != TextureImporterType.Sprite) { result = null; } else { if (textureImporter.spriteImportMode == SpriteImportMode.None) { textureImporter.spriteImportMode = SpriteImportMode.Single; AssetDatabase.WriteImportSettingsIfDirty(assetPath); SpriteUtility.ForcedImportFor(assetPath); } UnityEngine.Object @object = AssetDatabase.LoadAllAssetsAtPath(assetPath).FirstOrDefault((UnityEngine.Object t) => t is Sprite); result = (@object as Sprite); } return(result); }
public static List <Sprite> TextureToSprites(UnityEngine.Texture2D tex) { UnityEngine.Object[] array = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(tex)); List <Sprite> list = new List <Sprite>(); for (int i = 0; i < array.Length; i++) { if (array[i].GetType() == typeof(Sprite)) { list.Add(array[i] as Sprite); } } List <Sprite> result; if (list.Count > 0) { result = list; } else { Sprite sprite = SpriteUtility.GenerateDefaultSprite(tex); if (sprite != null) { list.Add(sprite); } result = list; } return(result); }
public static List <Sprite> GetSpriteFromPathsOrObjects(UnityEngine.Object[] objects, string[] paths, EventType currentEventType) { List <Sprite> list = new List <Sprite>(); for (int i = 0; i < objects.Length; i++) { UnityEngine.Object @object = objects[i]; if (AssetDatabase.Contains(@object)) { if (@object is Sprite) { list.Add(@object as Sprite); } else if (@object is UnityEngine.Texture2D) { list.AddRange(SpriteUtility.TextureToSprites(@object as UnityEngine.Texture2D)); } } } if (!SpriteUtility.ExistingAssets(objects) && currentEventType == EventType.DragPerform && EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D) { SpriteUtility.HandleExternalDrag(paths, true, ref list); } return(list); }
private static Sprite GenerateDefaultSprite(Texture2D texture) { string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object)texture); TextureImporter atPath = AssetImporter.GetAtPath(assetPath) as TextureImporter; if ((UnityEngine.Object)atPath == (UnityEngine.Object)null) { return((Sprite)null); } if (atPath.textureType != TextureImporterType.Sprite && atPath.textureType != TextureImporterType.Advanced) { return((Sprite)null); } if (atPath.spriteImportMode == SpriteImportMode.None) { if (atPath.textureType == TextureImporterType.Advanced) { return((Sprite)null); } atPath.spriteImportMode = SpriteImportMode.Single; AssetDatabase.WriteImportSettingsIfDirty(assetPath); SpriteUtility.ForcedImportFor(assetPath); } UnityEngine.Object @object = (UnityEngine.Object)null; try { @object = ((IEnumerable <UnityEngine.Object>)AssetDatabase.LoadAllAssetsAtPath(assetPath)).First <UnityEngine.Object>((Func <UnityEngine.Object, bool>)(t => t is Sprite)); } catch (Exception ex) { Debug.LogWarning((object)"Texture being dragged has no Sprites."); } return(@object as Sprite); }
private static Sprite HandleExternalDrag(bool perform) { if (DragAndDrop.paths.Length == 0) { return(null); } string text = DragAndDrop.paths[0]; if (!SpriteUtility.ValidPathForTextureAsset(text)) { return(null); } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (!perform) { return(null); } string text2 = AssetDatabase.GenerateUniqueAssetPath(Path.Combine("Assets", FileUtil.GetLastPathNameComponent(text))); if (text2.Length <= 0) { return(null); } FileUtil.CopyFileOrDirectory(text, text2); SpriteUtility.ForcedImportFor(text2); return(SpriteUtility.GenerateDefaultSprite(AssetDatabase.LoadMainAssetAtPath(text2) as Texture2D)); }
public static Sprite[] GetSpriteFromDraggedPathsOrObjects() { List <Sprite> list = new List <Sprite>(); UnityEngine.Object[] objectReferences = DragAndDrop.objectReferences; for (int i = 0; i < objectReferences.Length; i++) { UnityEngine.Object @object = objectReferences[i]; if (AssetDatabase.Contains(@object)) { if (@object is Sprite) { list.Add(@object as Sprite); } else { if (@object is Texture2D) { list.AddRange(SpriteUtility.TextureToSprites(@object as Texture2D)); } } } } if (list.Count > 0) { return(list.ToArray()); } return(new Sprite[] { SpriteUtility.HandleExternalDrag(Event.current.type == EventType.DragPerform) }); }
public static void AddAnimationToGO(GameObject go, Sprite[] frames) { if (frames != null && frames.Length > 0) { SpriteRenderer spriteRenderer = go.GetComponent <SpriteRenderer>(); if (spriteRenderer == null) { Debug.LogWarning("There should be a SpriteRenderer in dragged object"); spriteRenderer = go.AddComponent <SpriteRenderer>(); } spriteRenderer.sprite = frames[0]; if (frames.Length > 1) { Analytics.Event("Sprite Drag and Drop", "Drop multiple sprites to scene", "null", 1); if (!SpriteUtility.CreateAnimation(go, frames)) { Debug.LogError("Failed to create animation for dragged object"); } } else { Analytics.Event("Sprite Drag and Drop", "Drop single sprite to scene", "null", 1); } } }
private static Sprite GenerateDefaultSprite(Texture2D texture) { string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (textureImporter.textureType != TextureImporterType.Sprite && textureImporter.textureType != TextureImporterType.Advanced) { return(null); } if (textureImporter.spriteImportMode == SpriteImportMode.None) { if (textureImporter.textureType == TextureImporterType.Advanced) { return(null); } textureImporter.spriteImportMode = SpriteImportMode.Single; AssetDatabase.WriteImportSettingsIfDirty(assetPath); SpriteUtility.ForcedImportFor(assetPath); } UnityEngine.Object @object = null; try { @object = AssetDatabase.LoadAllAssetsAtPath(assetPath).First((UnityEngine.Object t) => t is Sprite); } catch (Exception) { Debug.LogWarning("Texture being dragged has no Sprites."); } return(@object as Sprite); }
private static bool CreateAnimation(GameObject gameObject, UnityEngine.Object[] frames) { Array.Sort <UnityEngine.Object>(frames, (UnityEngine.Object a, UnityEngine.Object b) => EditorUtility.NaturalCompare(a.name, b.name)); bool result; if (!AnimationWindowUtility.EnsureActiveAnimationPlayer(gameObject)) { result = false; } else { Animator closestAnimatorInParents = AnimationWindowUtility.GetClosestAnimatorInParents(gameObject.transform); if (closestAnimatorInParents == null) { result = false; } else { AnimationClip animationClip = AnimationWindowUtility.CreateNewClip(gameObject.name); if (animationClip == null) { result = false; } else { SpriteUtility.AddSpriteAnimationToClip(animationClip, frames); result = AnimationWindowUtility.AddClipToAnimatorComponent(closestAnimatorInParents, animationClip); } } } return(result); }
public static GameObject DropFramesToSceneToCreateGO(Sprite[] frames, Vector3 position) { if (frames.Length > 0) { Sprite sprite = frames[0]; string name = (!string.IsNullOrEmpty(sprite.name)) ? sprite.name : "Sprite"; name = GameObjectUtility.GetUniqueNameForSibling(null, name); GameObject gameObject = new GameObject(name); SpriteRenderer spriteRenderer = gameObject.AddComponent <SpriteRenderer>(); spriteRenderer.sprite = sprite; gameObject.transform.position = position; Selection.activeObject = gameObject; if (frames.Length > 1) { Analytics.Event("Sprite Drag and Drop", "Drop multiple sprites to scene", "null", 1); if (!SpriteUtility.CreateAnimation(gameObject, frames)) { UnityEngine.Object.DestroyImmediate(gameObject); return(null); } } else { Analytics.Event("Sprite Drag and Drop", "Drop single sprite to scene", "null", 1); } return(gameObject); } return(null); }
public static void AddAnimationToGO(GameObject go, Sprite[] frames) { if (frames == null || frames.Length <= 0) { return; } SpriteRenderer spriteRenderer = go.GetComponent <SpriteRenderer>(); if ((UnityEngine.Object)spriteRenderer == (UnityEngine.Object)null) { Debug.LogWarning((object)"There should be a SpriteRenderer in dragged object"); spriteRenderer = go.AddComponent <SpriteRenderer>(); } spriteRenderer.sprite = frames[0]; if (frames.Length > 1) { Analytics.Event("Sprite Drag and Drop", "Drop multiple sprites to scene", "null", 1); if (SpriteUtility.CreateAnimation(go, (UnityEngine.Object[])frames)) { return; } Debug.LogError((object)"Failed to create animation for dragged object"); } else { Analytics.Event("Sprite Drag and Drop", "Drop single sprite to scene", "null", 1); } }
public static UnityEngine.Texture2D RenderStaticPreview(Sprite sprite, Color color, int width, int height, Matrix4x4 transform) { UnityEngine.Texture2D result; if (sprite == null) { result = null; } else { PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)sprite.rect.width, (int)sprite.rect.height, ref width, ref height); SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState(); RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default); RenderTexture.active = temporary; GL.Clear(true, true, new Color(0f, 0f, 0f, 0.1f)); SpriteUtility.previewSpriteDefaultMaterial.mainTexture = sprite.texture; SpriteUtility.previewSpriteDefaultMaterial.SetPass(0); SpriteUtility.RenderSpriteImmediate(sprite, color, transform); UnityEngine.Texture2D texture2D = new UnityEngine.Texture2D(width, height, TextureFormat.ARGB32, false); texture2D.hideFlags = HideFlags.HideAndDontSave; texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0); texture2D.Apply(); RenderTexture.ReleaseTemporary(temporary); savedRenderTargetState.Restore(); result = texture2D; } return(result); }
public static void OnSceneDrag(SceneView sceneView) { Event current = Event.current; if (current.type != EventType.DragUpdated && current.type != EventType.DragPerform && current.type != EventType.DragExited) { return; } Sprite[] spriteFromDraggedPathsOrObjects = SpriteUtility.GetSpriteFromDraggedPathsOrObjects(); if (spriteFromDraggedPathsOrObjects.Length == 0) { return; } Sprite x = spriteFromDraggedPathsOrObjects[0]; if (x == null) { return; } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; EventType type = current.type; if (type == EventType.DragPerform) { Vector3 point = HandleUtility.GUIPointToWorldRay(current.mousePosition).GetPoint(10f); point.z = 0f; GameObject objectToUndo = SpriteUtility.DropFramesToSceneToCreateGO(spriteFromDraggedPathsOrObjects, point); Undo.RegisterCreatedObjectUndo(objectToUndo, "Create Sprite"); current.Use(); } }
public static Sprite TextureToSprite(Texture2D tex) { Sprite[] array = SpriteUtility.TextureToSprites(tex); if (array.Length > 0) { return(array[0]); } return(null); }
public static Sprite TextureToSprite(Texture2D tex) { Sprite[] sprites = SpriteUtility.TextureToSprites(tex); if (sprites.Length > 0) { return(sprites[0]); } return((Sprite)null); }
public static void OnSceneDrag(SceneView sceneView) { IEvent arg_2E_1 = new UnityEngine.U2D.Interface.Event(); UnityEngine.Object[] arg_2E_2 = DragAndDrop.objectReferences; string[] arg_2E_3 = DragAndDrop.paths; if (SpriteUtility.< > f__mg$cache0 == null) { SpriteUtility.< > f__mg$cache0 = new SpriteUtility.ShowFileDialogDelegate(EditorUtility.SaveFilePanelInProject); } SpriteUtility.HandleSpriteSceneDrag(sceneView, arg_2E_1, arg_2E_2, arg_2E_3, SpriteUtility.< > f__mg$cache0); }
private static void AddSpriteAnimationToClip(AnimationClip newClip, UnityEngine.Object[] frames) { newClip.frameRate = 12f; ObjectReferenceKeyframe[] keyframes = new ObjectReferenceKeyframe[frames.Length]; for (int index = 0; index < keyframes.Length; ++index) { keyframes[index] = new ObjectReferenceKeyframe(); keyframes[index].value = (UnityEngine.Object)SpriteUtility.RemapObjectToSprite(frames[index]); keyframes[index].time = (float)index / newClip.frameRate; } EditorCurveBinding binding = EditorCurveBinding.PPtrCurve(string.Empty, typeof(SpriteRenderer), "m_Sprite"); AnimationUtility.SetObjectReferenceCurve(newClip, binding, keyframes); }
private static void AddSpriteAnimationToClip(AnimationClip newClip, UnityEngine.Object[] frames) { newClip.frameRate = 12f; ObjectReferenceKeyframe[] array = new ObjectReferenceKeyframe[frames.Length]; for (int i = 0; i < array.Length; i++) { array[i] = default(ObjectReferenceKeyframe); array[i].value = SpriteUtility.RemapObjectToSprite(frames[i]); array[i].time = (float)i / newClip.frameRate; } EditorCurveBinding binding = EditorCurveBinding.PPtrCurve("", typeof(SpriteRenderer), "m_Sprite"); AnimationUtility.SetObjectReferenceCurve(newClip, binding, array); }
public static Sprite TextureToSprite(Texture2D tex) { Sprite[] array = SpriteUtility.TextureToSprites(tex); Sprite result; if (array.Length > 0) { result = array[0]; } else { result = null; } return(result); }
public static Sprite TextureToSprite(UnityEngine.Texture2D tex) { List <Sprite> list = SpriteUtility.TextureToSprites(tex); Sprite result; if (list.Count > 0) { result = list[0]; } else { result = null; } return(result); }
public ITexture2D GetReadableTexture2D() { if (this.m_ReadableTexture == null) { ITextureImporter assetImporterFromPath = this.m_AssetDatabase.GetAssetImporterFromPath(this.m_SelectedAssetPath); int width = 0; int height = 0; assetImporterFromPath.GetWidthAndHeight(ref width, ref height); this.m_ReadableTexture = SpriteUtility.CreateTemporaryDuplicate(this.m_OriginalTexture, width, height); if (this.m_ReadableTexture != null) { this.m_ReadableTexture.filterMode = FilterMode.Point; } } return(new UnityEngine.U2D.Interface.Texture2D(this.m_ReadableTexture)); }
public static void DoTilePreview(Sprite sprite, Color color, Matrix4x4 transform) { if (!(sprite == null)) { Rect totalPosition = EditorGUILayout.GetControlRect(false, 32f, new GUILayoutOption[0]); totalPosition = EditorGUI.PrefixLabel(totalPosition, new GUIContent(TileEditor.Styles.previewLabel)); Rect position = new Rect(totalPosition.xMin, totalPosition.yMin, 32f, 32f); Rect position2 = new Rect(totalPosition.xMin - 1f, totalPosition.yMin - 1f, 34f, 34f); if (Event.current.type == EventType.Repaint) { EditorStyles.textField.Draw(position2, false, false, false, false); } Texture2D image = SpriteUtility.RenderStaticPreview(sprite, color, 32, 32, transform); EditorGUI.DrawTextureTransparent(position, image, ScaleMode.StretchToFill); } }
public static Sprite[] GetSpritesFromDraggedObjects() { List <Sprite> spriteList = new List <Sprite>(); foreach (UnityEngine.Object objectReference in DragAndDrop.objectReferences) { if (objectReference.GetType() == typeof(Sprite)) { spriteList.Add(objectReference as Sprite); } else if (objectReference.GetType() == typeof(Texture2D)) { spriteList.AddRange((IEnumerable <Sprite>)SpriteUtility.TextureToSprites(objectReference as Texture2D)); } } return(spriteList.ToArray()); }
// Copy collider from sprite if it is drag&dropped to the inspector private void HandleDragAndDrop(Rect targetRect) { if (Event.current.type != EventType.DragPerform && Event.current.type != EventType.DragUpdated) { return; } // Check we're dropping onto the polygon collider editor. if (!targetRect.Contains(Event.current.mousePosition)) { return; } foreach (var obj in DragAndDrop.objectReferences.Where(obj => obj is Sprite || obj is Texture2D)) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (Event.current.type == EventType.DragPerform) { var sprite = obj is Sprite ? obj as Sprite : SpriteUtility.TextureToSprite(obj as Texture2D); // Copy collider to all selected components foreach (var collider in targets.Select(target => target as PolygonCollider2D)) { Vector2[][] paths; UnityEditor.Sprites.SpriteUtility.GenerateOutlineFromSprite(sprite, 0.25f, 200, true, out paths); collider.pathCount = paths.Length; for (int i = 0; i < paths.Length; ++i) { collider.SetPath(i, paths[i]); } DragAndDrop.AcceptDrag(); } if (EditorTools.EditorTools.activeToolType == typeof(PolygonCollider2DTool)) { EditorTools.EditorTools.RestorePreviousPersistentTool(); } } return; } DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; }
private void HandleDragAndDrop(Rect targetRect) { if (Event.current.type != EventType.DragPerform && Event.current.type != EventType.DragUpdated || !targetRect.Contains(Event.current.mousePosition)) { return; } using (IEnumerator <UnityEngine.Object> enumerator1 = ((IEnumerable <UnityEngine.Object>)DragAndDrop.objectReferences).Where <UnityEngine.Object>((Func <UnityEngine.Object, bool>)(obj => { if (!(obj is Sprite)) { return(obj is Texture2D); } return(true); })).GetEnumerator()) { if (enumerator1.MoveNext()) { UnityEngine.Object current1 = enumerator1.Current; DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (Event.current.type != EventType.DragPerform) { return; } Sprite sprite = !(current1 is Sprite) ? SpriteUtility.TextureToSprite(current1 as Texture2D) : current1 as Sprite; using (IEnumerator <PolygonCollider2D> enumerator2 = ((IEnumerable <UnityEngine.Object>) this.targets).Select <UnityEngine.Object, PolygonCollider2D>((Func <UnityEngine.Object, PolygonCollider2D>)(target => target as PolygonCollider2D)).GetEnumerator()) { while (enumerator2.MoveNext()) { PolygonCollider2D current2 = enumerator2.Current; Vector2[][] paths; UnityEditor.Sprites.SpriteUtility.GenerateOutlineFromSprite(sprite, 0.25f, (byte)200, true, out paths); current2.pathCount = paths.Length; for (int index = 0; index < paths.Length; ++index) { current2.SetPath(index, paths[index]); } this.m_PolyUtility.StopEditing(); DragAndDrop.AcceptDrag(); } return; } } } DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; }
private static void CreateSceneDragObjects(List <Sprite> sprites) { if (SpriteUtility.s_SceneDragObjects == null) { SpriteUtility.s_SceneDragObjects = new List <UnityEngine.Object>(); } if (SpriteUtility.s_DragType == SpriteUtility.DragType.CreateMultiple) { foreach (Sprite current in sprites) { SpriteUtility.s_SceneDragObjects.Add(SpriteUtility.CreateDragGO(current, Vector3.zero)); } } else { SpriteUtility.s_SceneDragObjects.Add(SpriteUtility.CreateDragGO(sprites[0], Vector3.zero)); } }