Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

CreatePrefab create a prefab attached with a script which has public variables

Discussion in 'Scripting' started by Lein, May 4, 2014.

  1. Lein

    Lein

    Joined:
    May 4, 2014
    Posts:
    14
    I wrote a c# script: TurnModeRPGAnimator.cs , it had some public variables: public List<Sprite> sprites,public Dictionary<int, int> curFramesMap,public int[] arr1; public int[][] arr2;

    then create a instance of TurnModeRPGAnimator, and assign values for sprites, curFramesMap, arr1, arr2;

    then I added it(the instance of TurnModeRPGAnimator) to a GameObject buy using AddComponent , the Create a Prefab of this GameObject,

    and then I load the prefab by using Resources.Load, then Instantiate it,

    in the method Update of TurnModeRPGAnimator.cs,

    I found sprites, arr1 has the right value I assigned to them, but curFramesMap,arr2 were null !

    I want to know, why curFramesMap,arr2 were null !

    Thanks!


    Code
    ------------------------------------------------------------
    Code (csharp):
    1.  
    2. //creating prefabs
    3.  
    4. using UnityEngine;  
    5. using UnityEditor;  
    6. using System.IO;
    7. using System;
    8. using System.Collections;
    9. using System.Collections.Generic;
    10.  
    11. public class LExportPrefabs
    12. {
    13.         private static string[] state_type = new string[]{"stand","attack","skill"};
    14.  
    15.         [MenuItem("Assets/+> Build Prefabs From Selection...")]
    16.         static void LExportResource ()
    17.         {
    18.                 //List<string> pathList = new List<string> ();  
    19.                 UnityEngine.Object[] objs = Selection.GetFiltered (typeof(UnityEngine.Object), SelectionMode.DeepAssets);  
    20.                 foreach (UnityEngine.Object _obj in objs) {  
    21.                         string path = AssetDatabase.GetAssetPath (_obj);
    22.                         Utils.log (path);
    23.                         if (Path.GetExtension (path) == ".png"  path.IndexOf (LAssetPostprocessor.autoPathMark) != -1) {
    24.                                 string filename = Path.GetFileName (path);
    25.                                 string spriteName = filename.Replace (".png", "");
    26.                                 //pathList.Add (path);
    27.                                 Utils.log (path + " creating prefab for " + spriteName + "...");
    28.                                 GameObject obj = new GameObject (spriteName);
    29.                                 obj.AddComponent<SpriteRenderer> ();
    30.                                 TurnModeRPGAnimator animator = obj.AddComponent<TurnModeRPGAnimator> ();
    31.                                 //animator.frameOrderOfState = new List<Dictionary<int, int>> ();
    32.                                 animator.frameOrderOfState.Add (new Dictionary<int, int> ());
    33.                                 animator.frameOrderOfState.Add (new Dictionary<int, int> ());
    34.                                 animator.frameOrderOfState.Add (new Dictionary<int, int> ());
    35.  
    36.                                 UnityEngine.Object[] arr = AssetDatabase.LoadAllAssetsAtPath (path);
    37.                                 int len = arr.Length;
    38.  
    39.                                 //List<Sprite> sprites = new List<Sprite> ();
    40.                                 Utils.log ("length: " + len);
    41.                                 for (int i=0; i<len; i++)
    42.                                         if (arr [i] is Sprite) {
    43.                                                 for (int j=0; j<3; j++)
    44.                                                         if (arr [i].name.IndexOf (state_type [j]) != -1) {
    45.                                                                 animator.frameOrderOfState [j].Add (Utils.parseInt (arr [i].name.Replace (state_type [j] + "_", "")), animator.sprites.Count);
    46.                                                                 animator.stand_list.Add (animator.sprites.Count);          
    47.                                                                 break;
    48.                                                         }
    49.                                                 animator.sprites.Add (arr [i] as Sprite);
    50.                                         }
    51.                                 //animator.frameOrderOfState = map;
    52.                                 animator.curFrameCount = animator.frameOrderOfState [0].Count;
    53.                                 //animator.curFramesMap = animator.frameOrderOfState [0];
    54.                                 animator.arr2 = new int[][]{
    55.                                     new int[]{1,2,3},new int[]{11,22,317}
    56.                                 };
    57.                                 animator.curFramesMap= new Dictionary<int, int>();
    58.                                 animator.curFramesMap.Add(1,1);
    59.                                 animator.curFramesMap.Add(2,2);
    60.                                 PrefabUtility.CreatePrefab ("Assets/Resources/Pets/" + spriteName + ".prefab", obj);
    61.                                 GameObject.DestroyImmediate (obj);
    62.                                 Utils.log ("OK");
    63.                         }
    64.                        
    65.                 }  
    66.  
    67.         }  
    68. }
    69.  
    70.  
    Code
    ------------------------------------------------------------
    Code (csharp):
    1.  
    2. //using prefabs
    3. using UnityEngine;
    4. using System.Collections;
    5. using System;
    6.  
    7. public class main : MonoBehaviour
    8. {
    9.  
    10.         // Use this for initialization
    11.         void Start ()
    12.         {
    13.                 UnityEngine.GameObject _obj = Resources.Load ("Pets/bb_1") as GameObject;
    14.                 if (_obj != null) {
    15.                         Utils.log ("Resources loaded OK");
    16.                 } else
    17.                         Utils.log ("Resources failt to load");
    18.                 Instantiate (_obj, new Vector3 (0, 0, 0), new Quaternion ());
    19.                 //GameObject.DestroyImmediate (_obj);
    20.                 /*      GameObject obj = new GameObject ("bb_1");
    21.         obj.AddComponent<SpriteRenderer> ();
    22.         TurnModeRPGAnimator animator = obj.AddComponent<TurnModeRPGAnimator> ();*/
    23.        
    24.                 /*UnityEngine.Object[] arr = AssetDatabase.LoadAllAssetsAtPath (path);
    25.         int len = arr.Length;
    26.         //List<Sprite> sprites = new List<Sprite> ();
    27.         Utils.log ("length: " + len);
    28.         for (int i=0; i<len; i++)
    29.         if (arr [i] is Sprite) {
    30.             animator.sprites.Add (arr [i] as Sprite);
    31.         }*/
    32.         }
    33.  
    34.         private static bool config_loaded = false;
    35.  
    36.         // Update is called once per frame
    37.         void Update ()
    38.         {
    39.    
    40.         }
    41. }
    42.  
    Code (csharp):
    1.  
    2. //TurnModeRPGAnimator.cs
    3.  
    4. using UnityEngine;
    5. using System;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8.  
    9. //using UnityEditor;
    10.  
    11.  
    12. public class TurnModeRPGAnimator : MonoBehaviour
    13. {
    14.         public List<Sprite> sprites = new List<Sprite> ();
    15.         public float framesPerSecond = 5;      
    16.         public float moveSpeed = 3;    
    17.         public float turnSpeed = 3;
    18.         public List<Dictionary<int, int>> frameOrderOfState=new List<Dictionary<int, int>>();
    19.        
    20.         public Dictionary<int, int> curFramesMap;
    21.         public int curFrameCount = 0;
    22.  
    23.  
    24.         public List<int> stand_list = new List<int>();
    25.         public int[][] arr2;
    26.    
    27.         private Vector3 moveDirection;
    28.         private SpriteRenderer spriteRenderer;
    29.  
    30.         public void changeState (int state)
    31.         {
    32.                 if (state < 0 || state > 2)
    33.                         return;
    34.                 curFramesMap = frameOrderOfState [state];
    35.                 curFrameCount = curFramesMap.Count;
    36.                 if (curFrameCount < 1) {
    37.                         Utils.log ("--- There is no frame! ---");
    38.                 }
    39.         }
    40.         // Use this for initialization
    41.         void Start ()
    42.         {
    43.                 /*Object[] arr = AssetDatabase.LoadAllAssetsAtPath ("Assets/Res/Pets/bb_1.png");
    44.                 int len = arr.Length;int i;
    45.                 for(i=0;i<len; i++)
    46.                     if(arr[i] is Sprite){
    47.                         sprites.Add(arr[i] as Sprite);
    48.                     }*/
    49.                
    50.                 moveDirection = Vector3.right;
    51. //              Animation an = new Animation ();
    52.                 //ModelImporterClipAnimationAA//
    53.                 spriteRenderer = renderer as SpriteRenderer;
    54.                 //changeState (0);
    55.         }
    56.        
    57.         //int[] arr = new int[]{0,5,6,7,8,9,10,11,12,1,2,3,4};
    58.         // Update is called once per frame
    59.         void Update ()
    60.         {
    61.                 if(frameOrderOfState==null){
    62.                     //Debug.Log("not found.............................");
    63.                     return;
    64.                 }
    65.                 int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
    66.                 //index = index % sprites.Count;
    67.                 index = index % curFrameCount;
    68.                 spriteRenderer.sprite = sprites [curFramesMap [index]];
    69.  
    70.                 // 1
    71.                 Vector3 currentPosition = transform.position;
    72.                 // 2
    73.                 if (Input.GetButton ("Fire1")) {
    74.                         // 3
    75.                         Vector3 moveToward = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    76.                         // 4
    77.                         moveDirection = moveToward - currentPosition;
    78.                         moveDirection.z = 0;
    79.                         moveDirection.Normalize ();
    80.            
    81.                         Vector3 target = moveDirection * moveSpeed + currentPosition;
    82.                         transform.position = Vector3.Lerp (currentPosition, target, Time.deltaTime);
    83.                 }
    84.        
    85.                 float targetAngle = Mathf.Atan2 (moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
    86.                 transform.rotation =
    87.                 Quaternion.Slerp (transform.rotation,
    88.                              Quaternion.Euler (0, 0, targetAngle),
    89.                              turnSpeed * Time.deltaTime);
    90.         }
    91. }
    92.  
    93.  
    :)
     
    Last edited: May 4, 2014
  2. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    Please use [c.o.d.e]Your code here[./.c.o.d.e] tags to post your code (without the dots) Because your code is unreadable like this
     
  3. Lein

    Lein

    Joined:
    May 4, 2014
    Posts:
    14
    That's nice, thanks
     
  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Multidimensional arrays and dictionaries can't be serialized.
     
  5. Lein

    Lein

    Joined:
    May 4, 2014
    Posts:
    14
    Oh, got it. I'll try to use simple variables