// This is the method to resize the actual children objects to fit within their new space public void AdjustChildren() { if (nestedBlockGroups != null && nestedBlockGroups.Count > 0) { for (int i = 0; i < nestedBlockGroups.Count; i++) { BlockGroup member = nestedBlockGroups[i]; float newX = (cellWidth * (member.xPosition - 1)) + (member.xPosition * padding); float newY = (cellHeight * (member.yPosition - 1)) + (member.yPosition * padding); float newW = (cellWidth * member.hPercent) + (padding * (member.hPercent - 1)); float newH = (cellHeight * member.vPercent) + (padding * (member.vPercent - 1)); RearrangeChildren(); RectTransform[] rts = nestedBlockGroups[i].GetComponentsInChildren<RectTransform>(); if (rts != null && rts.Length > 0) { foreach (RectTransform r in rts) { if (r != null) { // set the actual worldspace position based on the position of the parent. // This math sort of works but has/causes problems. // The biggest issue is that I didn't account for the pivot of the child and so everything is off by half the size of the object with the default RectTransform pivot of 0.5 // There might be other issues with multiple Blocks within a BlockGroup. I haven't tested it with that but it may adjust all the child Blocks of a BlockGroup to be the full size of the BlockGroup. // Lastly, this relies on the child objects having a RectTransform component. // You'd have to modify it to use scaling if you didn't want that and scaling has issues because of how it scales from the origin of the object and so as you scaled, you'd have to translate the object to keep it lined up. float actualX = xPos + newX; float actualY = yPos + newY; float actualZ = 0; // to keep it on the 2D plane Vector3 newPos = new Vector3(actualX, actualY, actualZ); r.SetPositionAndRotation(newPos, r.transform.rotation); r.sizeDelta = new Vector2(newW, newH); } } } } } }
} //END IsFocused //------------------------------// /// <summary> /// Check if a blockGroup has a block that has been selected. /// </summary> /// <param name="blockGroup">The blockGroup to check if it has a block that has been selected</param> /// <param name="checkNestedGroups">Should we also check any nested groups to see if they have a block that has been selected?</param> /// <returns></returns> public bool IsFocused( BlockGroup blockGroup, bool checkNestedGroups = true ) //------------------------------// { //If the blockGroup passed in is not null if( blockGroup != null ) { //And if the last block group selected is not empty if( this.blockGroup != null ) { //Then check if this is the currently selected blockGroup if( this.blockGroup == blockGroup ) { return true; } } } //So the passed in blockGroup was not the last selected, //let's check the nestedBlockGroups and see if any of them are the one that's selected if( checkNestedGroups ) { if( this.blockGroup != null && this.blockGroup.GetNestedBlockGroups() != null && this.blockGroup.GetNestedBlockGroups().Count > 0 ) { foreach( BlockGroup bGroup in this.blockGroup.GetNestedBlockGroups() ) { if( this.blockGroup == bGroup ) { return true; } } } } return false; } //END IsFocused
public void SetGroup( BlockGroup blockGroup ) { this.blockGroup = blockGroup; }
} //END AddBlockGroupForBlockIfNull //------------------------------------------------------------------// private static GameObject CreateBlock( GameObject blockReference, string name, BlockGroup group ) //------------------------------------------------------------------// { GameObject go = Instantiate( blockReference ); go.name = name; go.transform.SetParent( group.transform ); Debug.Log( "BXRCreateMenu.cs CreateBlock() Added new Block" + blockReference.GetComponent<Block>().GetBlockType() + " to " + group.name ); return go; } //END CreateBlock
} //END Update //--------------------------------// public virtual void Start() //--------------------------------// { //All BrandXR code relies heavily upon calling Prefabs, make sure we can do that! AddPrefabManagerIfNeeded(); //If the faceCameraTransform is null, try to find a child transform with the proper name and set it if( faceCameraTransform == null && transform.GetComponentInChildren<Transform>() != null && transform.GetComponentInChildren<Transform>().name == "FaceCamera" ) { faceCameraTransform = transform.GetComponentInChildren<Transform>(); } //Set our current blockGroup to null just to wipe the slate clean //We'll find the proper BlockGroup parent as our next step blockGroup = null; //All Blocks need to be the children of a BlockGroup //If we have a parent, and it does contain a BlockGroup, //then set that as our BlockGroup if( transform.parent != null && GetComponentInParent<BlockGroup>() != null ) { //Debug.Log( name + " Using existing BlockGroup parent" ); //We have a parent gameObject, and it already has a BlockGroup blockGroup = GetComponentInParent<BlockGroup>(); //If our parent gameObject has a generic name, it's safe to rename it to tidy up the editor hierarchy if( blockGroup.gameObject.name == "GameObject" ) { blockGroup.gameObject.name = "BlockGroup"; } } //If we have a parent, and it is a BlockView else if( transform.parent != null && transform.parent.GetComponent<BlockView>() != null ) { BlockView blockView = transform.parent.GetComponent<BlockView>(); //Add a intermediary BlockGroup in between the BlockView and the Block blockGroup = PrefabManager.InstantiatePrefab( PrefabFactory.Prefabs.bxr_BlockGroup ).GetComponent<BlockGroup>(); //Parent this block to the newly created BlockGroup this.transform.SetParent( blockGroup.transform ); //Parent the newly created BlockGroup to the blockView blockGroup.transform.SetParent( blockView.transform ); } //If we have a parent, but it does not contain a BlockGroup //And it is not a BlockView else if( transform.parent != null && transform.parent.GetComponent<BlockView>() == null ) { //Debug.Log( name + " Using Existing GameObject, adding new BlockGroup to GameObject" ); blockGroup = transform.parent.gameObject.AddComponent<BlockGroup>(); blockGroup._PrefabManager = _PrefabManager; //Tidy up the hierarchy naming if the gameObject name was generic if( blockGroup.gameObject.name == "GameObject" ) { blockGroup.gameObject.name = "BlockGroup"; } } //Otherwise, create a parent and give it a BlockGroup component else { //Debug.Log( name + " Creating new BlockGroup parent" ); //Add a BlockGroup to be the parent of this Block blockGroup = PrefabManager.InstantiatePrefab( PrefabFactory.Prefabs.bxr_BlockGroup ).GetComponent<BlockGroup>(); //If this component already has a parent, then set the newly created block to be a child of that gameObject if( transform.parent != null ) { blockGroup.transform.parent = transform.parent; } //Add this Block to be the child of this new BlockGroup transform.SetParent( blockGroup.transform ); //Tidy up the editor hierarchy naming if needed if( blockGroup.gameObject.name == "GameObject" ) { blockGroup.gameObject.name = "BlockGroup"; } } //Check if we should perform a command on Start() if( sendCommandOnStart ) { SendCommand( commandOnStart ); } } //END Start