-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScriptableTemplate.cs
More file actions
92 lines (76 loc) · 3.45 KB
/
ScriptableTemplate.cs
File metadata and controls
92 lines (76 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// =======================================================================================
// Wovencore
// by Weaver (Fhiz)
// MIT licensed
// =======================================================================================
using System;
using UnityEngine;
using Wovencode;
namespace Wovencode {
// ===================================================================================
// ScriptableTemplate
// ===================================================================================
public abstract partial class ScriptableTemplate : ScriptableObject
{
[Header("Basic Info")]
[Tooltip("This name will be displayed to the end-user (copied from object name if empty)")]
public string title;
// we never use the object name as the name that is shown to users
// instead we provide a custom string property called 'title'
// this way, you can rename your objects anytime without messing up
// the dictionary (as the dictionary keys are generated using the
// true name of the object)
[Tooltip("The name of a subfolder in the project 'Resources' folder (case-sensitive)")]
public string folderName;
// by specifying a subfolder, we can improve the Resources.LoadAll call a lot
// you just have to go sure that all templates of the corresponding type are
// in the correct subfolder
// we cache a few properties here for performance
protected string _name;
protected int _hash;
// -------------------------------------------------------------------------------
// BuildCache
// This methods builds the actual dictionary and fills it with data, its up to the
// derived class how to do that.
// -------------------------------------------------------------------------------
//public abstract static void BuildCache(bool force=false);
// -------------------------------------------------------------------------------
// name
// instead of returning the objects name each time it is accessed, we cache it in
// a local variable and return that one (less allocations)
// -------------------------------------------------------------------------------
public new string name
{
get {
if (string.IsNullOrWhiteSpace(_name))
_name = base.name;
return _name;
}
}
// -------------------------------------------------------------------------------
// hash
// instead of calculating the objects hash (from its name) everytime it is
// accessed, we cache it in a local variable instead and return that one
// -------------------------------------------------------------------------------
public int hash
{
get {
if (_hash == 0)
_hash = name.GetDeterministicHashCode();
return _hash;
}
}
// -------------------------------------------------------------------------------
// OnValidate
// if the title is empty, we simply copy the object name into the title
// note that we cannot cache the folderName here, because it would the same for all objects of this type
// -------------------------------------------------------------------------------
public virtual void OnValidate()
{
if (String.IsNullOrWhiteSpace(title))
title = name;
}
// -------------------------------------------------------------------------------
}
}
// =======================================================================================