Hi Unikron,
Thanks so much for the quick reply, that's solved the issue nicely! Please except my apologies, I'm new to editor scripts and didn't realise about this SetDirty() method.
In case anybody else is reading this, the entire call required to solve the issue in this case is:
UnityEditor.EditorUtility.SetDirty(tk2dSpriteCollectionData);
This tells Unity that the prefab object has changed and it should be saved to the disc. Then when you reload the editor the changes are kept. The requirement is nothing to do with tk2d, you need to do this whenever you change any prefab in Unity.
In case anybody wants to use this code, I've reposted it below with this line added. Just attach the MaterialChanger.cs script to any gameobject and it will work as a little tool for switching the material attached to a sprite.
// MaterialChanger.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MaterialChanger : MonoBehaviour {
public tk2dSpriteCollectionData data;
public string spriteContains;
public Material newMaterial;
public void ChangeMaterial () {
// Add material if necessary
List<Material> materials = new List<Material>(data.materials);
if (!materials.Contains(newMaterial)){
materials.Add(newMaterial);
data.materials = materials.ToArray();
}
// Get material index
int mIndex = 0;
for(int i =0; i<materials.Count;i++){
if (materials[i]==newMaterial){
mIndex = i;
break;
}
}
foreach (tk2dSpriteDefinition def in data.spriteDefinitions) {
if (def.name.Contains(spriteContains)){
def.material = newMaterial;
def.materialId = mIndex;
}
}
UnityEditor.EditorUtility.SetDirty(data);
}
}
// MaterialChangerEditor.cs - needs to be placed in a folder called "Editor"
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(MaterialChanger))]
public class MaterialChangerEditor : Editor {
public override void OnInspectorGUI() {
MaterialChanger materialChanger = (MaterialChanger) target;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Sprite collection data");
materialChanger.data = (tk2dSpriteCollectionData) EditorGUILayout.ObjectField(materialChanger.data,typeof(tk2dSpriteCollectionData),false);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Identifier");
materialChanger.spriteContains = EditorGUILayout.TextField(materialChanger.spriteContains);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Material to change to");
materialChanger.newMaterial = (Material)EditorGUILayout.ObjectField(materialChanger.newMaterial,typeof(Material),false);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Do change", GUILayout.MaxWidth(120))){
materialChanger.ChangeMaterial();
}
EditorGUILayout.EndHorizontal();
}
}