Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
cemuka committed Feb 27, 2021
0 parents commit 8897490
Show file tree
Hide file tree
Showing 33 changed files with 2,588 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@


# Created by https://www.toptal.com/developers/gitignore/api/unity
# Edit at https://www.toptal.com/developers/gitignore?templates=unity

### Unity ###
# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Autogenerated files
InitTestScene*.unity.meta
InitTestScene*.unity


# End of https://www.toptal.com/developers/gitignore/api/unity
56 changes: 56 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"files.exclude":
{
"**/.DS_Store":true,
"**/.git":true,
"**/.gitignore":true,
"**/.gitmodules":true,
"**/*.booproj":true,
"**/*.pidb":true,
"**/*.suo":true,
"**/*.user":true,
"**/*.userprefs":true,
"**/*.unityproj":true,
"**/*.dll":true,
"**/*.exe":true,
"**/*.pdf":true,
"**/*.mid":true,
"**/*.midi":true,
"**/*.wav":true,
"**/*.gif":true,
"**/*.ico":true,
"**/*.jpg":true,
"**/*.jpeg":true,
"**/*.png":true,
"**/*.psd":true,
"**/*.tga":true,
"**/*.tif":true,
"**/*.tiff":true,
"**/*.3ds":true,
"**/*.3DS":true,
"**/*.fbx":true,
"**/*.FBX":true,
"**/*.lxo":true,
"**/*.LXO":true,
"**/*.ma":true,
"**/*.MA":true,
"**/*.obj":true,
"**/*.OBJ":true,
"**/*.asset":true,
"**/*.cubemap":true,
"**/*.flare":true,
"**/*.mat":true,
"**/*.meta":true,
"**/*.prefab":true,
"**/*.unity":true,
"build/":true,
"Build/":true,
"Library/":true,
"library/":true,
"obj/":true,
"Obj/":true,
"ProjectSettings/":true,
"temp/":true,
"Temp/":true
}
}
8 changes: 8 additions & 0 deletions Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions Assets/Editor/ItemEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

public class ItemEditor : EditorWindow
{
private ScrollView _scrollView;
private List<ItemData> _savedItems = new List<ItemData>();

[MenuItem("Tools/Item Editor")]
public static void OpenWindow()
{
var window = GetWindow<ItemEditor>();
window.titleContent = new GUIContent("Item Editor");
}

private void OnEnable()
{
var root = this.rootVisualElement;
root.style.flexDirection = FlexDirection.Row;

var itemsListBox = new Box();
itemsListBox.style.flexGrow = 1f;
itemsListBox.style.flexShrink = 0f;
itemsListBox.style.flexBasis = 0f;
itemsListBox.style.flexDirection = FlexDirection.Column;

var newItemBox = new Box();
newItemBox.style.flexGrow = 3f;
newItemBox.style.flexShrink = 0f;
newItemBox.style.flexBasis = 0f;

SetupItemList(itemsListBox);
SetupFields(newItemBox);

root.Add(itemsListBox);
root.Add(newItemBox);

LoadData();
}

private void SetupFields(Box parent)
{
var newItemLabel = new Label("New Item");
newItemLabel.style.alignSelf = Align.Center;
newItemLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
parent.Add(newItemLabel);

var nameField = new TextField("Name: ");
var rarityField = new EnumField("Rarity: ", ItemRarity.Common);
var priceField = new IntegerField("Price: ");
parent.Add(nameField);
parent.Add(rarityField);
parent.Add(priceField);

var saveItemButton = new Button();
saveItemButton.text = "Save Item";
saveItemButton.clicked += () => {
if (string.IsNullOrWhiteSpace(nameField.value) == false)
{
var item = new ItemData();
item.id = new Guid().GetHashCode();
item.name = nameField.value;
item.rarity = (ItemRarity)rarityField.value;
item.price = priceField.value;

_savedItems.Add(item);

// set default values to clear fields
nameField.value = "";
rarityField.value = ItemRarity.Common;
priceField.value = 0;

CreateListItem(item);
SaveData();
}
};

parent.Add(saveItemButton);
}

private void SetupItemList(Box parent)
{
var listLabel = new Label("Item List");
listLabel.style.alignSelf = Align.Center;
listLabel.style.unityFontStyleAndWeight = FontStyle.Bold;
parent.Add(listLabel);

_scrollView = new ScrollView();
_scrollView.showHorizontal = false;
_scrollView.style.flexGrow = 1f;
parent.Add(_scrollView);
}

private void CreateListItem(ItemData itemData)
{
var itemElement = new VisualElement();
itemElement.style.flexDirection = FlexDirection.Row;
itemElement.focusable = true;

var remove = new Button();
remove.text = "-";
remove.clicked += () => {
_scrollView.contentContainer.Remove(itemElement);
_savedItems.Remove(itemData);
SaveData();
};
itemElement.Add(remove);

var nameButton = new Button();
nameButton.text = itemData.name;
nameButton.style.flexGrow = 1f;
itemElement.Add(nameButton);

_scrollView.contentContainer.Add(itemElement);
}

public void SaveData()
{
var path = Application.dataPath + "/Resources/items.json";

var itemsFile = new ItemsFile();
itemsFile.data = new List<ItemData>(_savedItems);
var itemsFileAsJson = JsonUtility.ToJson(itemsFile, true);
System.IO.File.WriteAllText(path, itemsFileAsJson);
}

public void LoadData()
{
var path = Application.dataPath + "/Resources/items.json";

if (System.IO.File.Exists(path))
{
var file = System.IO.File.ReadAllText(path);
_savedItems = JsonUtility.FromJson<ItemsFile>(file).data;

foreach (var item in _savedItems)
{
CreateListItem(item);
}
}
}
}

public enum ItemRarity
{
Common,
Rare,
Unique
}

[System.Serializable]
public class ItemData
{
public int id;
public string name;
public ItemRarity rarity;
public int price;
}

public class ItemsFile
{
public List<ItemData> data;
}
11 changes: 11 additions & 0 deletions Assets/Editor/ItemEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/Resources/items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"data": [
{
"id": 0,
"name": "sword",
"rarity": 0,
"price": 12
},
{
"id": 0,
"name": "pendant",
"rarity": 1,
"price": 300
},
{
"id": 0,
"name": "bloody axe",
"rarity": 2,
"price": 9000
},
{
"id": 0,
"name": "dagger",
"rarity": 1,
"price": 990
},
{
"id": 0,
"name": "wood",
"rarity": 0,
"price": 5
},
{
"id": 0,
"name": "bow",
"rarity": 0,
"price": 100
},
{
"id": 0,
"name": "arrow",
"rarity": 0,
"price": 3
}
]
}
7 changes: 7 additions & 0 deletions Assets/Resources/items.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Scenes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8897490

Please sign in to comment.