Beispiel #1
0
        public bool PopulateTreeView(AH_SerializedBuildInfo chosenBuildInfo)
        {
            //Todo, maybe not get ALL assets, but just the assets in the project folder (i.e. -meta etc)?
            treeElements = new List <AH_TreeviewElement>();

            int depth = -1;
            int id    = 0;

            var root = new AH_TreeviewElement("Root", depth, id, "", "", new List <string>(), false);

            treeElements.Add(root);

            depth++;
            id++;

            //This is done because I cant find all assets in toplevel folders through the Unity API (Remove whem the API allows it)
            int folderCount      = System.IO.Directory.GetDirectories(Application.dataPath).Count();
            int foldersProcessed = 0;

            bool populatedSuccesfully = AddFilesRecursively(Application.dataPath, chosenBuildInfo, depth, ref id, ref folderCount, ref foldersProcessed);

            //Cleanup garbage
            AssetDatabase.Refresh();
            GC.Collect();

            //Create tree
            if (populatedSuccesfully)
            {
                TreeElementUtility.ListToTree(treeElements);
            }

            EditorUtility.ClearProgressBar();

            return(populatedSuccesfully);
        }
Beispiel #2
0
    public static void TestListToTreeThrowsExceptionIfRootIsInvalidDepth()
    {
        // Arrange
        var list = new List <TestElement>();

        list.Add(new TestElement("root", 0));
        list.Add(new TestElement("A", 1));
        list.Add(new TestElement("B", 1));
        list.Add(new TestElement("Bchild", 2));

        // Test
        bool catchedException = false;

        try
        {
            TreeElementUtility.ListToTree(list);
        }
        catch (Exception)
        {
            catchedException = true;
        }

        // Assert
        Assert.IsTrue(catchedException, "We require the root.depth to be -1, here it is: " + list[0].depth);
    }
Beispiel #3
0
        public static Merge LoadFromJSON(this BehaviorTreeManagerAsset asset, BehaviorManager manager = null)
        {
            //TODO: Confirm reload from json
            if (asset == null)
            {
                Debug.Log("Asset is null when loading");
                return(new Merge("Empty Root", -1, -1));
            }
            else
            {
                //Elements should be a list of dynamic objects
                var elements = JsonConvert.DeserializeObject <List <dynamic> >(asset.RunnerElementsJSON);

                var newElements = new List <BehaviorTreeElement>();
                foreach (dynamic el in elements)
                {
                    string  typeName    = el.ElementType;
                    Type    type        = Assembly.GetAssembly(typeof(BehaviorTreeElement)).GetType(typeName);
                    dynamic newBehavior = Activator.CreateInstance(type, (string)el.Name, (int)el.Depth, (int)el.ID);

                    JsonConvert.PopulateObject(JsonConvert.SerializeObject(el), newBehavior);
                    newElements.Add(newBehavior);
                }
                var str = "";
                foreach (var e in newElements)
                {
                    str += e.Name + "\n";
                }

                var tree = TreeElementUtility.ListToTree(newElements);
                return((Merge)tree);
            }
        }
Beispiel #4
0
    public static void FindCommonAncestorsWithinListWorks()
    {
        // Arrange
        var list = new List <TestElement>();

        list.Add(new TestElement("root", -1));
        list.Add(new TestElement("A", 0));
        var b0 = new TestElement("B", 0);
        var b1 = new TestElement("Bchild", 1);
        var b2 = new TestElement("Bchildchild", 2);

        list.Add(b0);
        list.Add(b1);
        list.Add(b2);

        var c0 = new TestElement("C", 0);

        list.Add(c0);

        var f0 = new TestElement("F", 0);
        var f1 = new TestElement("Fchild", 1);
        var f2 = new TestElement("Fchildchild", 2);

        list.Add(f0);
        list.Add(f1);
        list.Add(f2);

        // Init tree structure: set children and parent properties
        TreeElementUtility.ListToTree(list);


        // Single element
        TestElement[] input          = { b1 };
        TestElement[] expectedResult = { b1 };
        var           result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();

        Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result), "Single input should return single output");

        // Single sub tree
        input          = new[] { b1, b2 };
        expectedResult = new[] { b1 };
        result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();
        Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result), "Common ancestor should only be b1 ");

        // Multiple sub trees
        input          = new[] { b0, b2, f0, f2, c0 };
        expectedResult = new[] { b0, f0, c0 };
        result         = TreeElementUtility.FindCommonAncestorsWithinList(input).ToArray();
        Assert.IsTrue(ArrayUtility.ArrayEquals(expectedResult, result), "Common ancestor should only be b0, f0, c0");
    }
Beispiel #5
0
    void Init(IList <T> data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data", "Input data is null. Ensure input is a non-null list.");
        }

        m_Data = data;
        if (m_Data.Count > 0)
        {
            m_Root = TreeElementUtility.ListToTree(data);
        }

        m_MaxID = m_Data.Max(e => e.id);
    }
Beispiel #6
0
    public static void TestListToTreeWorks()
    {
        // Arrange
        var list = new List <TestElement>();

        list.Add(new TestElement("root", -1));
        list.Add(new TestElement("A", 0));
        list.Add(new TestElement("B", 0));
        list.Add(new TestElement("Bchild", 1));
        list.Add(new TestElement("Bchildchild", 2));
        list.Add(new TestElement("C", 0));

        // Test
        TestElement root = TreeElementUtility.ListToTree(list);

        // Assert
        Assert.AreEqual("root", root.name);
        Assert.AreEqual(3, root.children.Count);
        Assert.AreEqual("C", root.children[2].name);
        Assert.AreEqual("Bchildchild", root.children[1].children[0].children[0].name);
    }