Skip to content

Instantly share code, notes, and snippets.

@onedayitwillmake
Last active August 29, 2015 14:04
Show Gist options
  • Save onedayitwillmake/2be960640cef1f07e747 to your computer and use it in GitHub Desktop.
Save onedayitwillmake/2be960640cef1f07e747 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections;
public class PCSAssetPlatformSwapper : MonoBehaviour {
/// <summary>
/// The name of the sprite collection.
/// NOTE: THIS IS AUTOMATICALLY SET FOR US WHEN WE MODIFY THE SPRITE BY 'PCSAssetPlatformSwapperEditor'
/// </summary>
[HideInInspector]
public string spriteCollectionName;
/// <summary>
/// The name of the sprite within the collection
/// NOTE: THIS IS AUTOMATICALLY SET FOR US WHEN WE MODIFY THE GAMEOBJECT IN THE EDITOR BY 'PCSAssetPlatformSwapperEditor'
/// </summary>
[HideInInspector]
public string spriteName;
/// <summary>
/// The logical dimensions of the sprite (not taking pixel density into account)
/// NOTE: THIS IS AUTOMATICALLY SET FOR US WHEN WE MODIFY THE GAMEOBJECT IN THE EDITOR BY 'PCSAssetPlatformSwapperEditor'
/// </summary>
[HideInInspector]
public Vector2 logicalDimensions;
private tk2dBaseSprite _cachedSprite;
private tk2dBaseSprite cachedSprite {
get {
if( _cachedSprite == null ){
_cachedSprite = gameObject.GetComponent<tk2dBaseSprite>();
}
return _cachedSprite;
}
}
/// <summary>
/// Swap the current collection with the current active one from the SpriteCollectionAssetManager
/// </summary>
public void Awake() {
// For whatever reason, it has not been set yet
// Attempt to retrieve the values from the live instance
if( string.IsNullOrEmpty(spriteCollectionName) || string.IsNullOrEmpty(spriteName) ) {
spriteCollectionName = PCSAssetUtil.StripAssetName(cachedSprite.Collection.name);
spriteName = cachedSprite.CurrentSprite.name;
}
tk2dSpriteCollectionData scd = PCSSpriteCollectionAssetManager.Instance.GetSpriteCollectionWithName(spriteCollectionName);
SetCollectionAndSprite( scd, spriteName);
}
/// <summary>
/// Sets the collection and sprite.
/// </summary>
/// <param name="aSpriteCollection">A sprite collection.</param>
/// <param name="aSpriteName">A sprite name.</param>
public void SetCollectionAndSprite( tk2dSpriteCollectionData aSpriteCollection, string aSpriteName ) {
// SpriteCollection is null, this happens when in the editor
// Maybe throw a warning?
if( aSpriteCollection == null ) {
return;
}
cachedSprite.SetSprite(aSpriteCollection, aSpriteName);
if (cachedSprite is tk2dSlicedSprite) {
((tk2dSlicedSprite)cachedSprite).dimensions = logicalDimensions * PCSAssetUtil.ASSET_MULTIPLIER;
} else if (cachedSprite is tk2dTiledSprite) {
((tk2dTiledSprite)cachedSprite).dimensions = logicalDimensions * PCSAssetUtil.ASSET_MULTIPLIER;
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CanEditMultipleObjects]
[CustomEditor(typeof(PCSAssetPlatformSwapper))]
public class PCSAssetPlatformSwapperEditor : Editor {
// Cache ref to AssetPlatformSwapper
private PCSAssetPlatformSwapper _platformSwapperComponent;
private PCSAssetPlatformSwapper platformSwapperComponent {
get {
if( _platformSwapperComponent == null ){
_platformSwapperComponent = (PCSAssetPlatformSwapper)target;
}
return _platformSwapperComponent;
}
}
public override void OnInspectorGUI() {
DrawDefaultInspector();
// Set the sprite collection, and spritename
tk2dBaseSprite sprite = platformSwapperComponent.GetComponent<tk2dBaseSprite>();
platformSwapperComponent.spriteCollectionName = PCSAssetUtil.StripAssetName(sprite.Collection.name);
platformSwapperComponent.spriteName = sprite.CurrentSprite.name;
// Store the logical dimensions of Sliced / Tiled sprites
float scale = PCSAssetUtil.GetAssetMultiplierForPlatform(sprite.Collection.name);
float inverseScale = 1.0f / scale;
if (sprite is tk2dSlicedSprite) {
Vector2 dimensions = ((tk2dSlicedSprite)sprite).dimensions;
platformSwapperComponent.logicalDimensions = dimensions * inverseScale;
} else if (sprite is tk2dTiledSprite) {
Vector2 dimensions = ((tk2dTiledSprite)sprite).dimensions;
platformSwapperComponent.logicalDimensions = dimensions * inverseScale;
}
EditorGUILayout.LabelField("SpriteCollection\t\t"+platformSwapperComponent.spriteCollectionName);
EditorGUILayout.LabelField("SpriteName\t\t\t\t"+platformSwapperComponent.spriteName);
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* EDIT TIME SWAPPING
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/// <summary>
/// Sets all textures to 1x.
/// </summary>
[MenuItem ("CashSlots/AssetSwapper/Set All To 1x")]
public static void SetAllTexturesTo1x() {
PCSAssetUtil.ASSET_MULTIPLIER = 1.0f;
SetSpriteCollectionSuffix("1x");
}
[MenuItem ("CashSlots/AssetSwapper/Set All To 2x")]
public static void SetAllTexturesTo2x() {
PCSAssetUtil.ASSET_MULTIPLIER = 2.0f;
SetSpriteCollectionSuffix("2x");
}
[MenuItem ("CashSlots/AssetSwapper/Set All To Ref Version")]
public static void SetAllTexturesToRef() {
PCSAssetUtil.ASSET_MULTIPLIER = 0.5f;
SetSpriteCollectionSuffix("Ref");
}
/// <summary>
/// Used by the CashSlots/AssetSwapper menu items
/// Sets the sprite to use the 'suffix' version of the collection (1x, 2x, Ref)
/// </summary>
/// <param name="suffix">Suffix.</param>
private static void SetSpriteCollectionSuffix(string suffix) {
var allSwappableSprites = UnityEngine.Object.FindSceneObjectsOfType(typeof(PCSAssetPlatformSwapper));
foreach( PCSAssetPlatformSwapper swappableSprite in allSwappableSprites ) {
string collectionName = PCSAssetUtil.StripAssetName( swappableSprite.spriteCollectionName );
// Find collection equivalent with the suffix we want
string[] searchResults = AssetDatabase.FindAssets( collectionName + suffix );
// We only care about the first - SpriteCollection1x.prefab file - not the data or etc
foreach( string result in searchResults ) {
string assetPath = AssetDatabase.GUIDToAssetPath(result);
if( assetPath.IndexOf(".prefab") > -1 && assetPath.IndexOf("Data") > -1 ) {
tk2dSpriteCollectionData scd = AssetDatabase.LoadAssetAtPath(assetPath, typeof(tk2dSpriteCollectionData) ) as tk2dSpriteCollectionData;
swappableSprite.SetCollectionAndSprite( scd, swappableSprite.spriteName );
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment